PHP Questions & Answers – Strings and Regular Expressions – 2

This set of PHP Interview Questions and Answers for Experienced people focuses on “Strings and Regular Expressions – 2”.

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

Answer: b
Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().

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

  1.     <?php
  2.     $foods = array("pasta", "steak", "fish", "potatoes");
  3.     $food = preg_grep("/^s/", $foods);
  4.     print_r($food);
  5.     ?>

a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )
View Answer

Answer: c
Explanation: This function is used to search an array for foods beginning with s.
advertisement
advertisement

3. Say we have two compare two strings which of the following function/functions can you use?

i) strcmp() 
ii) strcasecmp()
iii) strspn() 
iv) strcspn()

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

Answer: d
Explanation: All of the functions mentioned above can be used to compare strings in some or the other way.

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

Answer: a
Explanation: Its prototype follows string strtoupper(string str).
advertisement

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

  1.     <?php
  2.     $title = "O'malley wins the heavyweight championship!";
  3.     echo ucwords($title);
  4.     ?>

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

Answer: a
Explanation: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).
advertisement

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

  1.     <?php
  2.     echo str_pad("Salad", 5)." is good.";
  3.     ?>

a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good  Salad
d) Salad  is good
View Answer

Answer: d
Explanation: The str_pad() function pads a string with a specified number of characters.

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

Answer: b
Explanation: None.

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

Answer: d
Explanation: None.

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

  1.     <?php
  2.     $author = "[email protected]";
  3.     $author = str_replace("a","@",$author);
  4.     echo "Contact the author of this article at $author.";
  5.     ?>

a) Contact the author of this article at nachiketh@[email protected]
b) Cont@ct the @uthor of this @rticle @t n@chiketh@[email protected]
c) Contact the author of this article at n@chiketh@[email protected]
d) Error
View Answer

Answer: c
Explanation: The str_replace() function case sensitively replaces all instances of a string with another.

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

  1.     <?php
  2.     $url = "[email protected]";
  3.     echo ltrim(strstr($url, "@"),"@");
  4.     ?>

a) [email protected]
b) nachiketh
c) nachiketh@
d) example.com
View Answer

Answer: d
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP for Interviews, 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.