250+ TOP MCQs on PHP Introduction to Preg in PHP and Answers

PHP Multiple Choice Questions on “Introduction to Preg in PHP”.

1. What will be the output of the following PHP code?

  1.     
  2.     $number = array(0,1,two,three,four,5);
  3.     $num = preg_grep("/[0-5]/", $number);
  4.     print_r($num);
  5.     ?>

a) Array([0]=>0 [1]=>1 [2]=>two [3]=>three [4]=>four [5]=>5)
b) Array([2]=>two [3]=>three [4]=>four)
c) Array([1]=> 1)
d) Array([0]=>0 [1]=>1 [5]=>5)
Answer: d
Clarification: The preg_grep function is used to search an array for specific patterns and then return a new array based on that filtering.

2. 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([2]=>two [3]=>three [4]=>four)
c) Array([1]=> 1)
d) Array([0]=>0 [5]=>5)
Answer: b
Clarification: When we include PREG_GREP_INVERT, this will invert our data, so instead of outputting numbers it will output our non-numeric values.

3. Which one of the following functions are used to search a string?
a) preg_match
b) preg_search
c) preg_find
d) preg_found
Answer: a
Clarification: The function preg_match() searches string for pattern and it returns true if pattern exists, and false otherwise. The function returns 1 if search was successful else returns 0.

4. What will be the output of the following PHP code?

  1.     
  2.     $name = "What is your name?";
  3.     if (preg_match("/name/"),$name)
  4.     echo "My name is Will Pitt ";
  5.     else
  6.     echo "My name is not Will Pitt ";
  7.     if (preg_match("/are/"))
  8.     echo "I am great";
  9.     else
  10.     echo "I am not great";    
  11.     ?>

a) My name is Will Pitt I am great
b) My name is not Will Pitt I am great
c) My name is Will Pitt I am not great
d) My name is not Will Pitt I am not great

Answer: c
Clarification: The code uses preg_match to check for a keyword and replies based on whether it is true (1) or false (0).

5. Which one of the following preg PHP function is used to do a find and replace on a string or an array?
a) preg_replace()
b) preg_find()
c) preg_find_replace()
d) preg_findre()
Answer: a
Clarification: In preg_replace() function, after the replacement has occurred, the modified string will be returned and if no matches are found, the string will remain unchanged.

6. What will be the output of the following PHP code?

  1.     
  2.     $str = "Hello! My name is Cameron Fox. Coffee?";
  3.     $find = array('/is/','/coffee/');
  4.     $replace = array('/was/','/tea/');
  5.     echo preg_replace ($find, $replace, $str);
  6.     ?>

a) Hello! My name was Cameron Fox. tea?
b) Hello! My name is Cameron Fox. tea?
c) Hello! My name is Cameron Fox. Coffee?
d) Hello! My name was Cameron Fox. Coffee?
Answer: d
Clarification: Coffee was not replaced because the preg_replace function is case sensitive. Therefore it treats coffee and Coffee differently.

7. Which one of the following preg PHP functions is used to take a string, and put it in an array?
a) preg_destroy()
b) preg_split()
c) preg_unchain()
d) preg_divide()
Answer: b
Clarification: The string is broken up into different values in the array based upon your input.

8. What will be the output of the following PHP code?

  1.     
  2.     $line = "You like dogs. I hate dogs. We should marry.";
  3.     $sen = preg_split('/./', $line);
  4.     print_r($sen);
  5.     ?>

a) You like dogs. I hate dogs. We should marry.
b) Array([0]=>You like dogs. I hate dogs. We should marry.)
c) Array([0]=>You like dogs. [1]=>I hate dogs. [2]=>We should marry.)
d) Error

Answer: c
Clarification: We use a ‘.’ period to split the data, therefor giving each sentence it’s own array entry.

9. Which one of the following is not a preg PHP function?
a) preg_match
b) preg_match_all
c) preg_matchall
d) preg_split
Answer: c
Clarification: The function preg_match_all() matches all occurrences of pattern in string.

10. Parameter flags was added in which version of PHP?
a) PHP 4.0
b) PHP 4.1
c) PHP 4.2
d) PHP 4.3
Answer: d
Clarification: Parameter flags was added in PHP 4.3 version.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top