使用PHP正则表达式检查段落中的某些表达式
我想在PHP中使用正则表达式来检查文本是否至少有两段,第一段以“事实”开头,另一段以“我建议”开头 例如,我测试了这个:使用PHP正则表达式检查段落中的某些表达式,php,regex,Php,Regex,我想在PHP中使用正则表达式来检查文本是否至少有两段,第一段以“事实”开头,另一段以“我建议”开头 例如,我测试了这个: $text = "In fact Paris is more beautiful than Berlin. I suggest we go to Paris this summer." $value = preg_match ( "/ (^ In fact (.)) (I suggest). * /", $text); echo $value; I get $value
$text = "In fact Paris is more beautiful than Berlin.
I suggest we go to Paris this summer."
$value = preg_match ( "/ (^ In fact (.)) (I suggest). * /", $text);
echo $value;
I get $value = 0;
我得到$value=0
我不知道为什么,请帮忙。这应该行得通
$text = "In fact Paris is more beautiful than Berlin.
I suggest we go to Paris this summer.";
if(preg_match ( "/^In fact(.*)I suggest/s", $text)){
echo 'true';
} else {
echo 'false';
}
在正则表达式中,此*
允许任何字符
中的一个,然后允许任何数量的空格*
。此()
允许一个空格
我看不到你想捕获什么,所以我删除了你的捕获组。s
修饰符允许
匹配新行
正则表达式演示:PHP演示:您需要添加修饰符
\s
和量词*
()
\s
匹配任何空白字符,如新行串\n
和\r
。您的代码现在应该如下所示:
$value = preg_match ( "/(^In fact (.*))\s(I suggest(.*))/", $text);
现在,
$value
将为1。使用在线Regex测试员开发一些Regex foo:
$value = preg_match ( "/(^In fact (.*))\s(I suggest(.*))/", $text);