PHP Questions & Answers – Introduction to Preg in PHP

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Introduction to Preg in PHP”.

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

  1.     <?php
  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)
View Answer

Answer: d
Explanation: The preg_grep function is used to search an array for specific patterns and then return a new array based on that filtering.
advertisement
advertisement

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)
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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.     <?php
  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
View Answer

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

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()
View Answer

Answer: a
Explanation: 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.
advertisement

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

  1.     <?php
  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?
View Answer

Answer: d
Explanation: 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()
View Answer

Answer: b
Explanation: 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.     <?php
  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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: Parameter flags was added in PHP 4.3 version.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.