How to check if a string contains a specific text

Php

Php Problem Overview


<?php
$a = '';

if($a exist 'some text')
    echo 'text';
?>

Suppose I have the code above, how to write the statement if($a exist 'some text')?

Php Solutions


Solution 1 - Php

Use the strpos function: http://php.net/manual/en/function.strpos.php

$haystack = "foo bar baz";
$needle   = "bar";

if( strpos( $haystack, $needle ) !== false) {
    echo "\"bar\" exists in the haystack variable";
}

In your case:

if( strpos( $a, 'some text' ) !== false ) echo 'text';

Note that my use of the !== operator (instead of != false or == true or even just if( strpos( ... ) ) {) is because of the "truthy"/"falsy" nature of PHP's handling of the return value of strpos.

As of PHP 8.0.0 you can now use str_contains

<?php
    if (str_contains('abc', '')) {
        echo "Checking the existence of the empty string will always 
        return true";
    }

Solution 2 - Php

Empty strings are falsey, so you can just write:

if ($a) {
    echo 'text';
}

Although if you're asking if a particular substring exists in that string, you can use strpos() to do that:

if (strpos($a, 'some text') !== false) {
    echo 'text';
}

Solution 3 - Php

http://php.net/manual/en/function.strpos.php I think you are wondiner if 'some text' exists in the string right?

if(strpos( $a , 'some text' ) !== false)

Solution 4 - Php

If you need to know if a word exists in a string you can use this. As it is not clear from your question if you just want to know if the variable is a string or not. Where 'word' is the word you are searching in the string.

if (strpos($a,'word') !== false) {
echo 'true';
}

or use the is_string method. Whichs returns true or false on the given variable.

<?php
$a = '';
is_string($a);
?>

Solution 5 - Php

You can use strpos() or stripos() to check if the string contain the given needle. It will return the position where it was found, otherwise will return FALSE.

Use the operators === or `!== to differ FALSE from 0 in PHP.

Solution 6 - Php

You can use the == comparison operator to check if the variable is equal to the text:

if( $a == 'some text') {
    ...

You can also use strpos function to return the first occurrence of a string:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

See documentation

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucas FernandesView Question on Stackoverflow
Solution 1 - PhpDaiView Answer on Stackoverflow
Solution 2 - PhpBlenderView Answer on Stackoverflow
Solution 3 - PhpLeeishView Answer on Stackoverflow
Solution 4 - PhpJesseView Answer on Stackoverflow
Solution 5 - PhpHavenardView Answer on Stackoverflow
Solution 6 - PhpKermitView Answer on Stackoverflow