PHP Multiple Choice Questions – Strings and Regular Expressions

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Strings and Regular Expressions”.

1. PHP has long supported two regular expression implementations known as _______ and _______

i) Perl
ii) PEAR
iii) Pearl
iv) POSIX

a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)
View Answer

Answer: c
Explanation: None.

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

Answer: c
Explanation: None.

advertisement
advertisement

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

Answer: b
Explanation:[:alpha:] is nothing but Lowercase and uppercase alphabetical characters.

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

Answer: a
Explanation: ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase() are the functions offered.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

  1.     <?php
  2.     $username = "jasoN";
  3.     if (ereg("([^a-z])",$username))
  4.         echo "Username must be all lowercase!";
  5.     else
  6.         echo "Username is all lowercase!";
  7.     ?>

a) Error
b) Username must be all lowercase!
c) Username is all lowercase!
d) No Output is returned
View Answer

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

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

Answer: d
Explanation: None.
advertisement

7. POSIX stands for ____________
a) Portable Operating System Interface for Unix
b) Portable Operating System Interface for Linux
c) Portative Operating System Interface for Unix
d) Portative Operating System Interface for Linux
View Answer

Answer: a
Explanation: None.

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

  1.     <?php
  2.     $text = "this is\tsome text that\nwe might like to parse.";
  3.     print_r(split("[\n\t]",$text));
  4.     ?>

a) this is some text that we might like to parse.
b) Array ( [0] => some text that [1] => we might like to parse. )
c) Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
d) [0] => this is [1] => some text that [2] => we might like to parse.
View Answer

Answer: d
Explanation: The split() function divides a string into various elements, with the boundaries of each element based on the occurrence of a defined pattern within the string.

9. Which of the following would be a potential match for the Perl-based regular expression /fo{2,4}/?

i) fol
ii) fool
iii) fooool
iv) fooooool

a) Only i)
b) ii) and iii)
c) i), iii) and iv)
d) i) and iv)
View Answer

Answer: b
Explanation: This matches f followed by two to four occurrences of o.

10. Which among the following is/are not a metacharacter?

i) \a
ii) \A
iii) \b
iv) \B

a) Only i)
b) i) and iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer

Answer: a
Explanation: /A, /b and /B are metacharacters. \A: Matches only at the beginning of the string. \b: Matches a word boundary. \B: Matches anything but a word boundary.

More MCQs on PHP Strings and Regular Expressions:

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.