Php 查找某个字符串之后出现的字符串

Php 查找某个字符串之后出现的字符串,php,regex,string,preg-match,substring,Php,Regex,String,Preg Match,Substring,我试图检测电子邮件的优先级(以纯文本形式存储在一个变量中),每封电子邮件都有一个“评论”部分,我希望在评论区域搜索诸如“紧急”或“高”之类的术语,而不是其他任何地方,因为这类术语在其他地方 到目前为止,我一直在做的是: if (stristr($body, 'Comment: urgent')){ $urgent = true; echo '<b>Urgent.</b>'; } if(stristr($body,'Comment:emergency'))

我试图检测电子邮件的优先级(以纯文本形式存储在一个变量中),每封电子邮件都有一个“评论”部分,我希望在评论区域搜索诸如“紧急”或“高”之类的术语,而不是其他任何地方,因为这类术语在其他地方

到目前为止,我一直在做的是:

if (stristr($body, 'Comment: urgent')){
    $urgent = true;
    echo '<b>Urgent.</b>';
}
if(stristr($body,'Comment:emergency')){
$紧急=正确;
呼应“紧急”;
}
显然,这不适用于“紧急”出现在“this is紧迫”这样的句子中的情况

如何在子字符串“Comment:”后搜索$body


谢谢

您可以使用正则表达式:

<?php

$string = 'rweiuowreuiwuier Comment: higewrwre werwrewre high';

if (preg_match('#Comment: (urgent|high)#i',$string)){
    $urgent = true;
    echo '<b>Urgent.</b>';
}

您可以使用正则表达式:

<?php

$string = 'rweiuowreuiwuier Comment: higewrwre werwrewre high';

if (preg_match('#Comment: (urgent|high)#i',$string)){
    $urgent = true;
    echo '<b>Urgent.</b>';
}

您可以使用正则表达式:

<?php

$string = 'rweiuowreuiwuier Comment: higewrwre werwrewre high';

if (preg_match('#Comment: (urgent|high)#i',$string)){
    $urgent = true;
    echo '<b>Urgent.</b>';
}

您可以使用正则表达式:

<?php

$string = 'rweiuowreuiwuier Comment: higewrwre werwrewre high';

if (preg_match('#Comment: (urgent|high)#i',$string)){
    $urgent = true;
    echo '<b>Urgent.</b>';
}

您可以找到一个带有strpos函数的字符串

    $mystring = 'Comment: urgent';
    $find_string   = 'urgent';
    $pos = strpos($mystring, $find_string);

if ($pos !== false) {
     echo "I find it!";
} else {
     echo "Not found";
}

您可以使用strpos函数找到一个字符串

    $mystring = 'Comment: urgent';
    $find_string   = 'urgent';
    $pos = strpos($mystring, $find_string);

if ($pos !== false) {
     echo "I find it!";
} else {
     echo "Not found";
}

您可以使用strpos函数找到一个字符串

    $mystring = 'Comment: urgent';
    $find_string   = 'urgent';
    $pos = strpos($mystring, $find_string);

if ($pos !== false) {
     echo "I find it!";
} else {
     echo "Not found";
}

您可以使用strpos函数找到一个字符串

    $mystring = 'Comment: urgent';
    $find_string   = 'urgent';
    $pos = strpos($mystring, $find_string);

if ($pos !== false) {
     echo "I find it!";
} else {
     echo "Not found";
}

下面的函数(可以使用新名称)接受三个参数

$a being the Start Frame
$b being the End Frame
$s being the full string

$a would be 'Comment: '
$b would be whatever is at the end of your comments section
$s would be the email string
返回值将是中间的字符串,然后对返回值运行stripos

function getMiddle($a, $b, $s) {
    return strstr(substr($s, strpos($s, $a) + strlen($a)), $b, true);
}
示例:#注意,第二个参数需要专门用于您的电子邮件#


下面的函数(可以使用新名称)接受三个参数

$a being the Start Frame
$b being the End Frame
$s being the full string

$a would be 'Comment: '
$b would be whatever is at the end of your comments section
$s would be the email string
返回值将是中间的字符串,然后对返回值运行stripos

function getMiddle($a, $b, $s) {
    return strstr(substr($s, strpos($s, $a) + strlen($a)), $b, true);
}
示例:#注意,第二个参数需要专门用于您的电子邮件#


下面的函数(可以使用新名称)接受三个参数

$a being the Start Frame
$b being the End Frame
$s being the full string

$a would be 'Comment: '
$b would be whatever is at the end of your comments section
$s would be the email string
返回值将是中间的字符串,然后对返回值运行stripos

function getMiddle($a, $b, $s) {
    return strstr(substr($s, strpos($s, $a) + strlen($a)), $b, true);
}
示例:#注意,第二个参数需要专门用于您的电子邮件#


下面的函数(可以使用新名称)接受三个参数

$a being the Start Frame
$b being the End Frame
$s being the full string

$a would be 'Comment: '
$b would be whatever is at the end of your comments section
$s would be the email string
返回值将是中间的字符串,然后对返回值运行stripos

function getMiddle($a, $b, $s) {
    return strstr(substr($s, strpos($s, $a) + strlen($a)), $b, true);
}
示例:#注意,第二个参数需要专门用于您的电子邮件#



使用stripos()函数-使用stripos()函数-使用stripos()函数-使用stripos()函数-这并不能回答他应该如何仅从
注释:
第四条进行检测,因此不是正确的答案。因为他的字符串不仅仅是
Comment:urgent
,而是他想在“Comment:”字符串上找到urgent。不,'Comment:'是字符串的一部分,他想在该部分之后找到urgent,而不是在它之前。这并不能回答他应该如何仅从
Comment:
第四个位置进行检测,因此这不是一个正确的答案。因为他的字符串不仅仅是
Comment:urgent
,而是他想在“Comment:”字符串上找到urgent。不,'Comment:'是字符串的一部分,他想在该部分之后找到urgent,而不是在它之前。这并不能回答他应该如何仅从
Comment:
第四个位置进行检测,因此这不是一个正确的答案。因为他的字符串不仅仅是
Comment:urgent
,而是他想在“Comment:”字符串上找到urgent。不,'Comment:'是字符串的一部分,他想在该部分之后找到urgent,而不是在它之前。这并不能回答他应该如何仅从
Comment:
第四个位置进行检测,因此这不是一个正确的答案。因为他的字符串不仅仅是
Comment:urgent
,而是他想在“Comment:”字符串上找到加急。不,'Comment:'是字符串的一部分,他想在该部分后面找到加急,而不是在它之前。这很好,但很多人倾向于尽可能避免使用正则表达式,因此我的解决方案,请参阅答案;仍然投票,因为这是一个函数答案。很好的呼吁,但很多人倾向于尽量避免正则表达式,因此我的解决方案,见答案;仍然投票,因为这是一个函数答案。很好的呼吁,但很多人倾向于尽量避免正则表达式,因此我的解决方案,见答案;仍然投票,因为这是一个函数答案。很好的呼吁,但很多人倾向于尽量避免正则表达式,因此我的解决方案,见答案;由于这是一个函数答案,我仍然投票赞成。幸运的是,我有这个是为了我自己的需要,只是必须为自己最低限度地修改它:dLucky,我有这个是为了我自己的需要,只是必须为自己最低限度地修改它:dLucky,我有这个是为了我自己的需要,只是必须为自己对其进行最低限度的修改:D