PHP preg_match匹配相同标签的多个出现之间的文本

时间:2017-05-30 10:12:34

标签: php preg-match

如何匹配文字

$line = "study of 557 adults suffering from sleep";

来自

 $content = "The International Journal of Nursing published a
<b>study of 557 adults suffering</b>
<br>
<b>from sleep</b>
disorders. In this study, music was played when they ...";

并且内容可以是

 $content = "The International Journal of Nursing published a
<b>study of 557 adults suffering from sleep</b>
disorders. In this study, music was played when they ...";

所以我需要一个适用于两者的解决方案

preg_match("#<b>$line</b>#is",$content,$match);

3 个答案:

答案 0 :(得分:3)

喜欢这个? https://regex101.com/r/PDehGB/2

$pattern = '/<b>study.*?of.*?557.*?adults.*?suffering.*?from.*?sleep<\/b>/ms';

使用这种模式,它将处理任何新行并仍然保持$ line。

你可以爆炸&#34;上的$ line; &#34;并使用implode(".*?", $arr)

构建模式

答案 1 :(得分:0)

您可以使用此正则表达式(https://regex101.com/r/elRsha/2):

$line = "study of 557 adults suffering from sleep";

$content = "The International Journal of Nursing published a
<b>study of 557 adults suffering</b>
<br>
<b>from sleep</b>
disorders. In this study, music was played when they ...";

$line = implode('(?:[\n\s\t]*(?:<.+>)*[\n\s\t]*)*', explode(' ', $line));

preg_match("#($line)#is",$content,$match);
print_r($match);

答案 2 :(得分:0)

首先使用strip_tags函数从字符串中删除HTML标记,然后从字符串中删除换行符。

$line = "study of 557 adults suffering from sleep";

$content = "The International Journal of Nursing published a
<b>study of 557 adults suffering</b>
<br>
<b>from sleep</b>
disorders. In this study, music was played when they ...";

echo preg_match("#$line#is",trim(preg_replace('/\s+/', ' ', strip_tags($content))),$match);