Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 从正则表达式匹配中排除em标记_Php_Regex_Preg Replace - Fatal编程技术网

Php 从正则表达式匹配中排除em标记

Php 从正则表达式匹配中排除em标记,php,regex,preg-replace,Php,Regex,Preg Replace,我有以下html文本: <strong><em>La congiura della pietra nera</em></strong> <p><a href="xxxxx"> <img class="alignleft size-medium wp-image-75372" title="mytitle" src="my.jpg" alt="" width="247" height="350"></a

我有以下html文本:

<strong><em>La congiura della pietra nera</em></strong>
<p><a href="xxxxx">

<img class="alignleft size-medium wp-image-75372" title="mytitle" 

src="my.jpg" alt="" width="247" height="350"></a>

<strong>Trama:</strong> La storia ruota attorno ad una setta di guerrieri depositaria dei più arcani segreti.</p>


Trama:

我需要替换一些单词,我在php中使用以下正则表达式:

$mycontent = preg_replace('{'.$words.'(?![^<>]*>)}i','otherwords',$mycontent);
$mycontent=preg_replace(“{.$words.”(?![^]*>)}i',“otherwords',$mycontent);
它工作得很好,但我也需要排除标记内的文本,我如何才能做到这一点


非常感谢

请参阅以下示例代码:

    <?php
$foo = '<p><strong>SCHEDA FILM</strong>:<strong> <em>La congiura della pietra nera</em></strong></p>';
$bar1 = 'La congiura della pietra nera';
$bar2 = 'SCHEDA FILM';
echo preg_replace('/(<(?!em\b)(\w+)[^>]*>)'. $bar1 . '(<\/\2>)/', "$1do something$3", $foo);
//output '<p><strong>SCHEDA FILM</strong>:<strong> <em>La congiura della pietra nera</em></strong></p>'

echo preg_replace('/(<(?!em\b)(\w+)[^>]*>)'. $bar2 . '(<\/\2>)/', "$1do something$3", $foo);
//output '<p><strong>do something</strong>:<strong> <em>La congiura della pietra nera</em></strong></p>'     

?>


永远不要用正则表达式解析html。除了混淆解析和匹配之外,愚蠢的链接是相关的。使用规定的正则表达式方法,这只能通过过度的努力来实现。(你不能)。否则建议使用HTML/DOM遍历前端。