Regular Expressions(Php ) Questions and Answers

Question 1. What will be the output of the following PHP code?
  1.    [email protected]
  2.    contact
  3.    [email protected]
  4.    examveda.com
Explanation:-
Answer: Option D. -> examveda.com
The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.

Question 2. What will be the output of the following PHP code?
  1.    SaladSaladSaladSaladSalad is good
  2.    is good SaladSaladSaladSaladSalad
  3.    is good Salad
  4.    Salad is good
Explanation:-
Answer: Option D. -> Salad is good
The str_pad() function pads a string with a specified number of characters.

Question 3. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
  1.    explode()
  2.    implode()
  3.    concat()
  4.    concatenate()
Explanation:-
Answer: Option B. -> implode()

Question 4. What will be the output of the following PHP code?
  1.    Contact the author of this article at [email protected]@mple.com
  2.    [email protected] the @uthor of this @rticle @t [email protected]@[email protected]
  3.    Contact the author of this article at [email protected]@[email protected]
  4.    Error
Explanation:-
Answer: Option C. -> Contact the author of this article at [email protected]@[email protected]
The str_replace() function case sensitively replaces all instances of a string with another.

Question 5. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
  1.    strlastpos()
  2.    strpos()
  3.    strlast()
  4.    strrpos()
Explanation:-
Answer: Option D. -> strrpos()

Question 6. PHP has long supported two regular expression implementations known as _______ and _______.
1. Perl
2. PEAR
3. Pearl
4. POSIX
  1.    1 and 2
  2.    2 and 4
  3.    1 and 4
  4.    2 and 3
Explanation:-
Answer: Option C. -> 1 and 4

Question 7. How many functions does PHP offer for searching strings using POSIX style regular expression?
  1.    7
  2.    8
  3.    9
  4.    10
Explanation:-
Answer: Option A. -> 7
ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase() are the functions offered.

Question 8. Which one of the following regular expression matches any string containing zero or one p?
  1.    p+
  2.    p*
  3.    P?
  4.    p#
Explanation:-
Answer: Option C. -> P?

Question 9. [:alpha:] can also be specified as.
  1.    [A-Za-z0-9]
  2.    [A-za-z]
  3.    [A-z]
  4.    [a-z]
Explanation:-
Answer: Option B. -> [A-za-z]
[:alpha:] is nothing but Lowercase and uppercase alphabetical characters.

Question 10. What will be the output of the following PHP code?
  1.    Error
  2.    Username must be all lowercase!
  3.    Username is all lowercase!
  4.    No Output is returned
Explanation:-
Answer: Option B. -> Username must be all lowercase!
Because the provided username is not all lowercase, ereg() will not return FALSE (instead returning the length of the matched string, which PHP will treat as TRUE), causing the message to output.