w3resource
php Programming Exercies

PHP String Exercises: Check if a string contains a specific string

PHP String: Exercise-3 with Solution

Write a PHP script to check if a string contains a specific string?

Sample string : 'The quick brown fox jumps over the lazy dog.'
Check whether the said string contains the string 'jumps'.

Pictorial Presentation:

PHP String Exercises: Check if a string contains a specific string

Sample Solution:

Code:

<?php
$str1 = 'The quick brown fox jumps over the lazy dog.';
if (strpos($str1,'jumps') !== false) 
 {
    echo 'The specific word is present.';
 }
else
 {
    echo 'The specific word is not present.';
 }
?>

Sample Output:

The specific word is present.

Flowchart :

Flowchart: Check if a string contains a specific string

PHP Code Editor:

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

Previous: Write a PHP script to split the specific string.
Next: Write a PHP script to convert the value of a PHP variable to string.

What is the difficulty level of this exercise?



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