2017-05-30 54 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);