PHP Regular Expressions | PHP Questions

Que. Which one of the following functions is used to search a string?
a. preg_found
b. preg_match
c. preg_search
d. preg_find
Answer: preg_match


Que. How many functions does PHP offer for searching strings using POSIX style regular expression?
a. 8
b. 9
c. 10
d. 7
Answer: 7


Que. Which one of the following functions will convert a string to all uppercase?
a. str_uppercase()
b. uppercase()
c. struppercase()
d. strtoupper()
Answer: strtoupper()


Que. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
a. explode()
b. concat()
c. concatenate()
d. implode()
Answer: implode()


Que. What will be the output of the following PHP code?
$username = "jasoN";
if (ereg("([^a-z])",$username))
echo "Username must be all lowercase!";
else
echo "Username is all lowercase!";
?>
a. Error
b. Username must be all lowercase!
c. No Output is returned
d. Username is all lowercase!


Username must be all lowercase!


Que. What will be the output of the following PHP code?
$number = array(0,1,two,three,four,5);
$num = preg_grep("/[0-5]/", $number);
print_r($num);
?>
a. Array([0]=>0 [1]=>1 [2]=>two [3]=>three [4]=>four [5]=>5)
b. Array([1]=> 1)
c. Array([0]=>0 [1]=>1 [5]=>5)
d. Array([2]=>two [3]=>three [4]=>four)
Answer: Array([0]=>0 [1]=>1 [5]=>5)


Que. Which of the following would be a potential match for the Perl-based regular expression /fo{2,4}/ ?
1. fol
2. fool
3. fooool
4. fooooool
a. Only 1
b. 2 and 3
c. 1, 3 and 4
d. 1 and 4
Answer: 2 and 3


Que. POSIX implementation was deprecated in which version of PHP?
a. PHP 4
b. PHP 5.3
c. PHP 5.2
d. PHP 5
Answer: PHP 5.3


Que. What will be the output of the following PHP code?
$text = "this istsome text thatnwe might like to parse.";
print_r(split("[nt]",$text));
?>
a. this is some text that we might like to parse.
b. [0] => this is [1] => some text that [2] => we might like to parse.
c. Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
d. Array ( [0] => some text that [1] => we might like to parse. )
Answer: [0] => this is [1] => some text that [2] => we might like to parse.


Que. Say we have two compare two strings which of the following function/functions can you use?
1. strcmp()
2. strcasecmp()
3. strspn()
4. strcspn()
a. None of the mentioned
b. All of the mentioned
c. 3 and 4
d. 1 and 2


All of the mentioned


Que. What will be the output if we replace the line $num = preg_grep(“/[0-5]/”, $number); with $num = preg_grep(“/[0-5]/”, $number, PREG_GREP_INVERT);?
a. Array([0]=>0 [1]=>1 [2]=>two [3]=>three [4]=>four [5]=>5)
b. Array([1]=> 1)
c. Array([2]=>two [3]=>three [4]=>four)
d. Array([0]=>0 [5]=>5)
Answer: Array([2]=>two [3]=>three [4]=>four)


Que. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
a. strlast()
b. strpos()
c. strlastpos()
d. strrpos()
Answer: strrpos()


Que. [:alpha:] can also be specified as.
a. [A-z]
b. [A-Za-z0-9]
c. [a-z]
d. [A-za-z]
Answer: [A-za-z]


Que. What will be the output of the following PHP code?
$name = "What is your name?"
if (preg_match("/name/"),$name)
echo "My name is Will Pitt ";
else
echo "My name is not Will Pitt ";
if (preg_match("/are/"))
echo "I am great"
else
echo "I am not great";
?>
a. My name is not Will Pitt I am great
b. My name is Will Pitt I am not great
c. My name is Will Pitt I am great
d. My name is not Will Pitt I am not great
Answer: My name is Will Pitt I am not great


Que. Which one of the following regular expression matches any string containing zero or one p?
a. P?
b. p#
c. p+
d. p*


P?


Que. POSIX stands for
a. Portative Operating System Interface for Unix
b. Portable Operating System Interface for Linux
c. Portative Operating System Interface for Linux
d. Portable Operating System Interface for Unix
Answer: Portable Operating System Interface for Unix


Que. What will be the output of the following PHP code?
$foods = array("pasta", "steak", "fish", "potatoes");
$food = preg_grep("/^s/", $foods);
print_r($food);
?>
a. Array ( [3] => potatoes )
b. Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
c. Array ( [1] => steak )
d. Array ( [0] => potatoes )
Answer: Array ( [1] => steak )


Que. What will be the output of the following PHP code?
$title = "O'malley wins the heavyweight championship!";
echo ucwords($title);
?>
a. O’Malley Wins The Heavyweight Championship!
b. O’Malley wins the heavyweight championship!
c. o’malley wins the heavyweight championship!
d. O’malley Wins The Heavyweight Championship!
Answer:


Que. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular expressions.
a. 9
b. 7
c. 8
d. 10
Answer: 8


Que. PHP has long supported two regular expression implementations known as _______ and _______.
1. Perl
2. PEAR
3. Pearl
4. POSIX
a. 2 and 3
b. 1 and 2
c. 1 and 4
d. 2 and 4


1 and 4


Que. What will be the output of the following PHP code?
$author = "[email protected]";
$author = str_replace("a","@",$author);
echo "Contact the author of this article at $author.";
?>
a. Error
b. [email protected] the @uthor of this @rticle @t [email protected]@[email protected]
c. Contact the author of this article at [email protected]@mple.com
d. Contact the author of this article at [email protected]@[email protected]
Answer: Contact the author of this article at [email protected]@[email protected]


Que. What will be the output of the following PHP code?
$url = "[email protected]";
echo ltrim(strstr($url, "@"),"@");
?>
a. contact
b. [email protected]
c. examveda.com
d. [email protected]
Answer: examveda.com


Que. Which among the following is/are not a metacharacter?
1. /a
2. /A
3. /b
4. /B
a. 2 and 4
b. 2, 3 and 4
c. 1 and 3
d. Only 1
Answer: Only 1


Que. What will be the output of the following PHP code?
echo str_pad("Salad", 5)." is good.";
?>
a. is good SaladSaladSaladSaladSalad
b. is good Salad
c. SaladSaladSaladSaladSalad is good
d. Salad is good
Answer: Salad is good


Que. Which one of the following preg PHP function is used to do a find and replace on a string or an array?
a. preg_find()
b. preg_findre()
c. preg_replace()
d. preg_find_replace()


preg_replace()


Comments