PHP操作符来查看某个对象是否包含某个对象的一部分
我现在有个问题。我有一个类似下面的代码,我需要一个操作符来检查数组中是否存在查询的一部分。这是我的代码:PHP操作符来查看某个对象是否包含某个对象的一部分,php,preg-match,Php,Preg Match,我现在有个问题。我有一个类似下面的代码,我需要一个操作符来检查数组中是否存在查询的一部分。这是我的代码: <?php $search = 'party hat'; $query = ucwords($search); $string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php'); $string = explode('<br>',$string); f
<?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>";
}
}
?>
我想做的不是如果$matches[1]==$query要检查两者是否相同,我想让我的代码查看$query的一部分是否存在于$matches[1]中。我该怎么做呢?请帮帮我 您可以使用测试一个字符串是否包含在另一个字符串中:
if(strpos($matches[1], $query) !== false)
if(strstr($matches[1], $query)) {
// do something ...
}
如果希望它不区分大小写,请改用。如果要检查$query是否是$matches[1]的子字符串,可以使用 strpos$匹配项[1],$query!==假的
有关必须使用!==的原因,请参阅文档 您可以使用strstr测试一个字符串是否包含另一个字符串:
if(strpos($matches[1], $query) !== false)
if(strstr($matches[1], $query)) {
// do something ...
}
您需要更具体一点:每个字符串都有一个存在于其他字符串中的部分。例如,XX中两个X之间的空字符串可以在ABC中的A之前找到。