我想在ckeditor中删除默认包装img标签的p标签。
我不想完全禁用 p 标签或将 EnterMode 更改为另一个标签。我只想阻止图像被包裹在段落中。
我想要这个完成客户端,而不是服务器端。
我有:
<p>Some text in a parapgraph.</p>
<p><img src="picture.jpg"></p>
<p>Another paragraph</p>
我想:
<p>Some text in a parapgraph.</p>
<img src="picture.jpg">
<p>Another paragraph</p>
最佳答案
快速修复:
如果除了三个标签之外没有其他内容,这将起作用。
$str = "<p><img src=\"/file.jpg\" width=\"1\" height=\"2\" /></p>"
$replaced = preg_replace ( "/<p[^>]*?>(<img[^>]+>)<\/p>/" , "$1" , $str )
更新:
这是一个PHP函数。您可以从 JavaScript 调用它。
function filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');
Reference
关于javascript - 从 ckeditor 中的 img 中删除包装 p,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992265/