php 使用 preg_match 在字符串中查找短语

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5613527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:59:25  来源:igfitidea点击:

Find phrase in string with preg_match

phpstringpreg-match

提问by RobRoy

I am searching through text line by line and want to see if the line contains the phrase "see details" and is not case sensitive, so will find:

我正在逐行搜索文本并想查看该行是否包含短语“查看详细信息”并且不区分大小写,因此会发现:

See Details, See details, SEE Details etc

查看详情、查看详情、查看详情等

I have this so far.

到目前为止我有这个。

if(preg_match("/^(\see details)/", strtolower($line)))
{
    echo 'SEE DETAILS FOUND';
}

A simple example would be helpful thanks.

一个简单的例子会很有帮助,谢谢。

回答by Pascal MARTIN

If you want to check if a sub-string is present in a string, no need for regular expressions : stripos()will do just fine :

如果要检查字符串中是否存在子字符串,则不需要正则表达式:stripos()就可以了:

if (stripos(strtolower($line), 'see details') !== false) {
    // 'see details' is in the $line
}


stripos()will return the position of the first occurrence of the sub-string in the string ; or falseif the sub-string is not found.


stripos()将返回子字符串在字符串中第一次出现的位置;或者false如果未找到子字符串。

Which means that if it returns something else than false, the sub-string is found.

这意味着如果它返回的不是false,则找到子字符串。

回答by Charles

Your regex is actually broken.

您的正则表达式实际上已损坏。

/^(\see details)/

This breaks down into:

这分解为:

  • At the beginning
  • Open a capturing group
  • Look for one whitespace character
  • Followed by all of the following characters: ee details
  • Close the group
  • 一开始
  • 打开一个捕获组
  • 查找一个空白字符
  • 后跟以下所有字符: ee details
  • 关闭群组

\sis an escape sequencematching whitespace. You could also have added the imodifierto make the regex case-insensitive. You also don't seem to be doing anything with the captured group, so you can ditch that.

\s匹配空格的转义序列。你也可以添加i修改,使正则表达式不区分大小写。你似乎也没有对被捕获的群体做任何事情,所以你可以放弃它。

Therefore:

所以:

/^see details/i

is what you'd want.

是你想要的。

You mentioned that you're going through input line by line. If you only need to know that the entire input contains the specific string, and you have the input as a string, you can use the mmodifier to make ^match "beginning of a line" instead of / in addition to "beginning of the string":

你提到你正在逐行处理输入。如果您只需要知道整个输入包含特定字符串,并且您将输入作为字符串,则可以使用m修饰符来^匹配“行首”而不是 / 除了“字符串的开头” :

/^see details/im

If this is the case, then you would end up with:

如果是这种情况,那么您最终会得到:

if(preg_match('/^see details/im', $whole_input)) {
    echo "See Details Found!";
}

But as others have mentioned, a regex isn't needed here. You can (and should) do the job with the more simple stripos.

但正如其他人所提到的,这里不需要正则表达式。您可以(并且应该)使用更简单的stripos.

回答by LooPer

As Pascal said, you can use stripos()function although correct code would be:

正如帕斯卡所说,您可以使用stripos()函数,尽管正确的代码是:

if (stripos(strtolower($line), 'see details') !== false) {
    // 'see details' is in the $line
}

回答by Alejandro Moreno

accoding to the php documentation (http://www.php.net/manual/en/function.preg-match.php):

根据 php 文档(http://www.php.net/manual/en/function.preg-match.php):

<?php
/* The \b in the pattern indicates a word boundary, so only the distinct
 * word "web" is matched, and not a word partial like "webbing" or "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

which it looks pretty simple and nice :-).

它看起来非常简单和漂亮:-)。