w3resource
php Programming Exercies

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

PHP regular expression: Exercise-1 with Solution

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

Pictorial Presentation:

PHP Regular Expression Exercise: Checks if 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 if 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?



New Content: Composer: Dependency manager for PHP, R Programming