S17514
S17514

Reputation: 285

PHP operator to see if something has part of something

I currently have a problem. I have a code that looks like the one below and I want an operator that checks if a part of a query is present in an array. This is the code that I have:

<?php
$search = 'party hat';
$query = ucwords($search);
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
$string = explode('<br>',$string);
foreach($string as $row)
{
    preg_match('/^(\D+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
    if($matches[1] == "$query")
    {
        echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
        echo $matches[1];
        echo "</a><br>";
    }
}
?>

What I want to do is instead of if($matches[1] == "$query") to check if both are identical, I want my code to see if a PART of $query exists in $matches[1]. How do I do this though? Please help me!

Upvotes: 0

Views: 129

Answers (3)

Stefan Fandler
Stefan Fandler

Reputation: 1141

You can use strstr to test if a string contains another string:

if(strstr($matches[1], $query)) {
    // do something ...
}

Upvotes: 0

Jay
Jay

Reputation: 3305

If you want to check if $query is a substring of $matches[1], you can use

strpos($matches[1], $query) !== false

(see documentation for why you must use !==).

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318498

You can use strpos to test if a string is contained in another string:

if(strpos($matches[1], $query) !== false)

If you prefer it to be case insensitive, use stripos instead.

Upvotes: 5

Related Questions