php - How do I check if a string contains a specific word?


Consider:

$a = 'How are you?';

if ($a contains 'are')
    echo 'true';

Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?


Answers



You can use the strpos() function which is used to find the occurrence of one string inside another one:

$a = 'How are you?';

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

Note that the use of !== false is deliberate; strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').




You could use regular expressions. It would look something like this:

$a = 'How are you?';

if (preg_match('/are/',$a))
    echo 'true';

Don't tell me it's bad just because you've heard it's bad before. You might if you have any facts to back it up though ;)

On the performance side, strpos is about three times faster and have in mind, when I did one million compares at once, it took preg match 1.5 seconds to finish and for strpos it took 0.5 seconds. What I'm trying to say is that it runs really fast either way. If you don't have 100,000 visitors every second, you shouldn't concern yourself with this kind of stuff and take what's most comfortable, IMO.




Use strpos function:

if (strpos($a, 'are') !== false)
    echo 'true';



PHP - How to check if a string contain any text

Use strpos

$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.




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';
}






Does array contain (part of) string?

Loop through the $forbiddennames array and use stripos to check if the given input string matches any of the items in the array:

function is_forbidden($forbiddennames, $stringtocheck) 
{
    foreach ($forbiddennames as $name) {
        if (stripos($stringtocheck, $name) !== FALSE) {
            return true;
        }
    }
}

And use it like below:

if(is_forbidden($forbiddennames, $stringtocheck)) {
    echo "This is a forbidden username.";
} else {
    echo "True";
}

Demo!




foreach ($forbiddennames as $forbiddenname) {
    $nametocheck = strtolower($stringtocheck);
    if(strpos($stringtocheck, $forbiddenname) !== false) {
        echo "This is a forbidden username.";
        break;
    }
}



It doesn't really matter if you use array_map, foreach or something different. Possible solution:

$forbiddenNames = array('admin', 'bannedName');
$input = 'Admin12';
$allowed = true;
foreach($forbiddenNames as $forbiddenName) {
    if(stripos($input, $forbiddenName) !== false) {
        echo $input, ' is invalid';
        $allowed = false;
        break;
    }
}
if($allowed === true) {
    echo $input, ' is valid';
}



PHP if not in string

I always use this:

if (stristr($string, "string")) {
    //String exists within $string
}else{
    //String does not exist
}

http://www.php.net/stristr




Try this:

if(strpos($string, 'String') !== false) {
    //string exist
}
else{
    //string not exist
}