w3resource

PHP Regular Expression Exercises: Checks whether a string contains another string

PHP regular expression: Exercise-1 with Solution

Write a PHP script that checks whether a string contains another string.

Pictorial Presentation:

PHP Regular Expression Exercise: Checks whether a string contains another string

Sample Solution:

PHP Code:

<?php
$pattern = '/[^\w]fox\s/';
if (preg_match($pattern, 'The quick brown fox jumps over the lazy dog'))
  {
  echo "'fox' is present..."."\n";
  }
  else
  echo "'fox' is not present..."."\n";
?>

Sample Output:

'fox' is present... 

Flowchart :

Flowchart: Checks whether a string contains another string.

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: PHP Regular Expression Exercises Home.
Next: Write a PHP script that removes the last word from a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.