好的,所以我试图检测某个字符串是否在另一个字符串中,我已经尝试使用过,explode
但是由于明显的原因而无法正常工作,这样您就可以更好地了解我要完成的工作,看看我尝试explode
用来解决我的问题的失败尝试
$stringExample1 = "hi, this is a example";
$stringExample2 = "hello again, hi, this is a example, hello again";
$expString = explode($stringExample1, $stringExample2);
if(isset($expString[1]))
{
//$stringExample1 is within $stringExample2 !!
}
else
{
//$stringExample1 is not within $stringExample2 :(
}
任何帮助将不胜感激谢谢
在这里strpos
,并strstr
因为你有额外的逗号未能example2
在第二个字符串。我们可以使用正则表达式进行字符串匹配。例如,请参见下面的代码片段。
<?php
$str1 = "hi, this is a example";
$str2 = "hello again, hi, this is a example, hello again";
$pattern = "/$str1/";
preg_match_all($pattern, $str2, $matches);
print_r($matches);
输出将是
Array
(
[0] => Array
(
[0] => hi, this is a example
)
)
如果输出数组($matches
)的count大于0,则表示匹配,否则不匹配。您可能需要调整创建的正则表达式$pattern
以适合您的需要,并且可能还需要优化。
让我们知道这是否对您有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句