我希望将所见即所得编辑器中的标签替换为。
目前,我正在使用以下代码来实现这一点。
$content = preg_replace('/<h1(.*?)<\/h1>/si', '<p class="heading-1"$1</p>', $content);
$content = preg_replace('/<h2(.*?)<\/h2>/si', '<p class="heading-2"$1</p>', $content);
$content = preg_replace('/<h3(.*?)<\/h3>/si', '<p class="heading-3"$1</p>', $content);
$content = preg_replace('/<h4(.*?)<\/h4>/si', '<p class="heading-4"$1</p>', $content);
$content = preg_replace('/<h5(.*?)<\/h5>/si', '<p class="heading-5"$1</p>', $content);
$content = preg_replace('/<h6(.*?)<\/h6>/si', '<p class="heading-6"$1</p>', $content);正如你所看到的,这段代码相当混乱,如果我能把它压缩成一个正则表达式,那就太好了,但我只是缺乏这样做的能力。
我曾考虑过将此行代码作为替代方案。
$content = preg_replace('/<h(.*?)<\/h(.*?)>/si', '<p class="heading-$2"$1</p>', $content);我不确定是否使用上面的内容,客户倾向于从其他网站复制内容,直接粘贴到他们新的WYSIWYG中,我看到从hr标签到标题标签的任何东西都会在那里弹出。
我需要的只是上面的一行,除了标签本身只能是2个特定的字符(所以要确保标签以H开头,后面跟1-6)。
我还要求它添加到p标记的类是特定于数字使用的,例如: heading-1,heading-2。
任何帮助都将不胜感激,感谢您的宝贵时间。
发布于 2013-06-06 17:42:53
$content = <<<HTML
<h1 class="heading-title">test1</h1>
<H2 class="green">test2</H2>
<h5 class="red">test</h5>
<h5 class="">test test</h5>
HTML;
$content = preg_replace('#<h([1-6]).*?class="(.*?)".*?>(.*?)<\/h[1-6]>#si', '<p class="heading-${1} ${2}">${3}</p>', $content);
echo htmlentities($content);结果:
<p class="heading-1 heading-title">test1</p>
<p class="heading-2 green">test2</p>
<p class="heading-5 red">test</p>
<p class="heading-5 ">test test</p>现有类注意事项:即使您的元素没有现有类,也必须添加空类属性class=""。相反,这将不会像预期的那样工作。:(更好的解决方案是使用。然后,您可以检查是否存在匹配项,并更准确地创建p tags。
https://stackoverflow.com/questions/16958490
复制相似问题