php – 如何正则表达式字符<和>替换像<和>在标签?
发布时间:2020-12-13 21:55:04 所属栏目:PHP教程 来源:网络整理
导读:我有一个像bellow的字符串: pre title="language-markup" code div title="item_content item_view_content" itemprop="articleBody" abc /div /code/pre 在 code / code中标签我想替换所有字符和与 lt;和 gt;.我应该怎么做? 示例: code 安培; LT; div gt
我有一个像bellow的字符串:
<pre title="language-markup"> <code> <div title="item_content item_view_content" itemprop="articleBody"> abc </div> </code> </pre> 在< code>< / code>中标签我想替换所有字符<和>与& lt;和& gt;.我应该怎么做? 示例:< code> &安培; LT; div& gt;< code>. 如果您有任何想法,请告诉我.谢谢大家. 解决方法
尝试以下解决方案
$textToScan = '<pre title="language-markup"> <code> <div title="item_content item_view_content" itemprop="articleBody"> abc </div> </code> </pre>'; // the regex pattern (case insensitive & multiline $search = "~<code>(.*?)</code>~is"; // first look for all CODE tags and their content preg_match_all($search,$textToScan,$matches); //print_r($matches); // now replace all the CODE tags and their content with a htmlspecialchars() content foreach($matches[1] as $match){ $replace = htmlspecialchars($match); // now replace the previously found CODE block $textToScan = str_replace($match,$replace,$textToScan); } // output result echo $textToScan; 输出: <pre title="language-markup"> <code> <div title="item_content item_view_content" itemprop="articleBody"> abc </div> </code> </pre> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |