Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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
Regex preg_替换正则表达式HTML_Regex_Wordpress_Tinymce_Preg Replace - Fatal编程技术网

Regex preg_替换正则表达式HTML

Regex preg_替换正则表达式HTML,regex,wordpress,tinymce,preg-replace,Regex,Wordpress,Tinymce,Preg Replace,我在WordPress中使用短代码。在每个短代码输出结束div之后,我得到了或标记。 试图过滤掉它们,但我不知道怎么过滤。生成的HTML看起来像 <div class="fullwidth"><!-- 1st shortcode--> <div class="fullwidth-content"> <!-- 2nd shortcode--> <div class="twocol-one"> content </div&

我在WordPress中使用短代码。在每个短代码输出结束div之后,我得到了或标记。 试图过滤掉它们,但我不知道怎么过滤。生成的HTML看起来像

<div class="fullwidth"><!-- 1st shortcode-->
<div class="fullwidth-content">
  <!-- 2nd shortcode-->
  <div class="twocol-one"> content
  </div><br>
</div><br>
<!-- 3rd shortcode-->
<div class="twocol-second"> content
</div><br>
<div class="clearboth"></div>
</div><br>
似乎BR是tinyMCE的新词。而且我不想要冗长的代码行

我试图使用preg_replace,但无法创建正确的$pattern

你能帮我吗

我的职能

function replace_br($content) {
$rep = preg_replace("/<\/div>\s*<br\s*\/?>/i", "</div>",$content);
return $rep; }
add_filter('the_content', 'replace_br');
不工作

使用时 $rep=preg\u replace/\s*/i$content;在功能上,所有BR均被更换。 很好,但我只想在关闭DIV标记后替换BRs

stru替换$content;也不起作用

我的功能有什么问题


没有返回错误。

您首先做错了,因为您必须删除标记。 你这样做是错误的,因为你在HTML中使用正则表达式,有时它是OK的

您使用的正则表达式的变体应该足够:

您应该考虑使用DOMBOOST或类似:

$html = <<<HTML
...
HTML;

$dom = new DOMDocument();

$dom->loadHTML($html);

$element = $dom->getElementsByTagName('br');

$remove = [];
foreach($element as $item){
  $remove[] = $item;
}

foreach ($remove as $item) {
  $item->parentNode->removeChild($item); 
}

$html = $dom->saveHTML();

echo $html;

这将删除所有br,您需要调整规范的代码工作,但这应该是一个指针。

这是使用regex的另一种方法 就你而言


我们再来一遍,一遍,一遍…你的正则表达式没有问题,可能是wordpress在后面格式化了代码,检查一下。欢迎来到wordpress。启动step调试器,并准确地找出将这些标记放入的过滤器函数。然后关闭过滤器。但是要小心,wordpress可能会因为你删除了一个过滤器而崩溃。我不确定是否使用DOMDocumentclass@hakre谢谢,你帮了我很多。这是WP过滤器,在进行短代码之前在[shortcode]之后添加,所以我过滤掉了靠近[shortcodes]`$shortcodes=join |,arraymyshortcode1,myshortcode2,另一个短代码;//开始标记$rep=preg_replace/?[$shortcodes\s[^]+?]|?/,[$2$3],‌​$内容;//结束标记$rep=preg\u replace/?[\/$shortcodes]|?/,[/$2],$rep`
/(?<=<\/div>)(<br[\s\/]?>)/mg