Write a php script to check whether a string contains a specific string

Last update on August 19 2022 21:50:29 [UTC/GMT +8 hours]

PHP String: Exercise-3 with Solution

Write a PHP script to check whether 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:

Sample Solution:

PHP Code:



Sample Output:

The specific word is present.

Flowchart :

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.

PHP: Tips of the Day

PHP: How to use array_filter[] to filter array keys?

PHP 5.6 introduced a third parameter to array_filter[], flag, that you can set to ARRAY_FILTER_USE_KEY to filter by key instead of value:

$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed  = ['foo', 'bar'];
$filtered = array_filter[
    $my_array,
    function [$key] use [$allowed] {
        return in_array[$key, $allowed];
    },
    ARRAY_FILTER_USE_KEY
];

Clearly this isn't as elegant as array_intersect_key[$my_array, array_flip[$allowed]], but it does offer the additional flexibility of performing an arbitrary test against the key, e.g. $allowed could contain regex patterns instead of plain strings.

You can also use ARRAY_FILTER_USE_BOTH to have both the value and the key passed to your filter function. Here's a contrived example based upon the first, but note that I'd not recommend encoding filtering rules using $allowed this way:

$my_array = ['foo' => 1, 'bar' => 'baz', 'hello' => 'wld'];
$allowed  = ['foo' => true, 'bar' => true, 'hello' => 'world'];
$filtered = array_filter[
    $my_array,
    function [$val, $key] use [$allowed] { // N.b. $val, $key not $key, $val
        return isset[$allowed[$key]] && [
            $allowed[$key] === true || $allowed[$key] === $val
        ];
    },
    ARRAY_FILTER_USE_BOTH
]; // ['foo' => 1, 'bar' => 'baz']

Ref : //bit.ly/3iUn1l5

Topic: PHP / MySQLPrev|Next

Answer: Use the PHP strpos[] Function

You can use the PHP strpos[] function to check whether a string contains a specific word or not.

The strpos[] function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false. Also note that string positions start at 0, and not 1.

Let's check out an example to understand how this function basically works:

Related FAQ

Here are some more FAQ related to this topic:

  • How to count how many times a substring occurs in a string in PHP
  • How to replace a word inside a string in PHP
  • How to split a string into an array in PHP

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    A string is a collection of given characters and a substring is a string present in a given string.

    In this article, we are going to check if the given string contains a substring by using the PHP strpos[]function.

    Syntax:

    strpos[$original_string,$sub_string];

    Parameters:

    • original_string : The input string
    • sub_string : The substring searched in the original input string.

    Return type: Boolean value

    • True, If substring found
    • False, If substring not found.

    Example:

    PHP

    Output:

    True

    Example 2:

    PHP

    Output:

    False

    Example 3: The following example also considers space.

    PHP

    Output:

    True

    Example 4:

    PHP

    Output:

    False

    How do you check if a string contains a string in PHP?

    Answer: Use the PHP strpos[] Function You can use the PHP strpos[] function to check whether a string contains a specific word or not. The strpos[] function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

    How will you search a string in PHP?

    PHP's strstr[] function simply takes a string to search, and a chunk of text to search for. If the text was found, it returns the portion of the string from the first character of the match up to the end of the string: $myString = 'Hello, there!'; echo strstr[ $myString, 'llo' ]; // Displays "llo, there!"

    Does string contain PHP?

    The str_contains is a new function that was introduced in PHP 8. This method is used to check if a PHP string contains a substring. The function checks the string and returns a boolean true in case it exists and false otherwise. However, keep in mind that str_contains is case-sensitive.

    How do you check if a string starts with a substring in PHP?

    We can test if a certain string is the exact start of another string: You can always RegEx too!

    Chủ Đề