Php 如果字符串在另一个字符串中

Php 如果字符串在另一个字符串中,php,string,parsing,Php,String,Parsing,好的,我正在尝试检测某个字符串是否在另一个字符串中,我已经尝试过使用explode,但由于明显的原因,它不起作用,只是为了让您能够更好地了解我试图完成的任务,请看一看我使用explode尝试解决我的问题的失败尝试 $stringExample1 = "hi, this is a example"; $stringExample2 = "hello again, hi, this is a example, hello again"; $expString = explode($stringE

好的,我正在尝试检测某个字符串是否在另一个字符串中,我已经尝试过使用
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);
如果输出数组(
$matches
)的计数大于0,则我们有匹配项,否则我们没有匹配项。您可能需要调整在
$pattern
中创建的正则表达式以满足您的需要,还可能需要优化


让我们知道这是否适用于您。

此处
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);
如果输出数组(
$matches
)的计数大于0,则我们有匹配项,否则我们没有匹配项。您可能需要调整在
$pattern
中创建的正则表达式以满足您的需要,还可能需要优化


让我们知道这是否适用于您。

在我的情况下,$findme的可能副本将包括用空格分隔的单词。。。strpos的使用仍然有效吗?@JeffCoderr请看我的答案,并告诉我们这是否是您想要的。我已经测试了
strpo
strstr
这两个选项,它们在您的情况下不起作用。在我的情况下,$findme将包括用空格分隔的单词。。。strpos的使用仍然有效吗?@JeffCoderr请看我的答案,并告诉我们这是否是您想要的。我已经测试了
strpo
strstr
这两个选项,它们在您的案例中都不起作用。谢谢回答:)谢谢回答:)
if (strlen(strstr($stringExample2,$stringExample1))>0) {
    echo 'true';
}
else
{
    echo 'false';
}
<?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
        )

)