Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP如何使用preg_匹配选择接近word的内容_Php_Preg Match - Fatal编程技术网

PHP如何使用preg_匹配选择接近word的内容

PHP如何使用preg_匹配选择接近word的内容,php,preg-match,Php,Preg Match,我有一个字符串,比如: $str = "Hello, I'm a beautiful, string. How are you?"; 我想知道美丽的下一个字。我的意思是,我想要一根绳子。 我认为这比使用迭代器数组和比较每个单词更好。。。逗号、点和斜线必须省略。 我对此一无所知。。。如果可能的话。您可以使用: 例如,在PHP中: preg_match('~beautiful[^a-zA-Z\d.]*([a-zA-Z\d]+)~', $str, $results); echo $results[1

我有一个字符串,比如:

$str = "Hello, I'm a beautiful, string. How are you?";
我想知道美丽的下一个字。我的意思是,我想要一根绳子。 我认为这比使用迭代器数组和比较每个单词更好。。。逗号、点和斜线必须省略。 我对此一无所知。。。如果可能的话。

您可以使用:

例如,在PHP中:

preg_match('~beautiful[^a-zA-Z\d.]*([a-zA-Z\d]+)~', $str, $results);
echo $results[1]; // string
细分:

~ 
    beautiful        // match "beautiful literally
    [^a-zA-Z\d]*     // don't match any letters or digits zero or more (catch punctuation etc)
    (                // open capture group
        [a-zA-Z\d]+  // match letters or numbers at least once
    )                // close capture group (stops at first occurence that 
~                    //   doesn't match above     

您可以使用以下内容:

\bbeautiful\b\W*\b(\w+)\b
这有点像:

将你的单词与两边的单词边界相匹配 匹配任意数量的非单词字符 将下一个单词与两侧的单词边界匹配
.

在美丽之后,直到第一次出现a之前,你想要的东西总是一根线吗。?基本上,括号中的内容都是字符?还是会有数字或特殊字符$str=你好,我是一个美丽的、可爱的女孩。你好吗@杰伊:不管字符串是否以点结尾。。。我所知道的唯一想法是他的话。例如在Hello中,我是另一个例子中的第二个foo;例如,我想在“秒”之后得到str,我希望得到str foo
\bbeautiful\b\W*\b(\w+)\b