2017-05-30 55 views
1

如何从PHP的preg_match匹配的文本

$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 = "study of 557 adults suffering from sleep"; 

和somtimes内容可以

$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); 
+0

因此,你想在所有的大胆标签之间? – Andreas

+1

查看相关的问题 – Jelmergu

+0

我想在两个$内容中匹配$ line与单个解决方案 –

回答

3

是否这样? https://regex101.com/r/PDehGB/2

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

有了这种模式将采取任何新的线路护理,仍然保持在$线。

可以发生爆炸“的” $线,并与implode(".*?", $arr)

+1

对我来说似乎更加正确。 +1 –

+0

@AlivetoDie谢谢! :-) – Andreas

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); 
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);