如何创建正则表达式以向特定元素添加额外的html标记?

时间:2016-02-21 03:56:14

标签: php regex wordpress

我正在研究Wordpress主题,并且要求是这样的。

  1. 在打印页面内容时,将DIV包装<div class="image-wrapper">添加到所有图像。我已经做了。

  2. 在打印页面内容时,将DIV包装器添加到除图像之外的其他文本DIV包装器<div class="text-wrapper">

  3. 这样做的目的是我想应用一个样式,以便除了图像之外的所有文本都有左右边距。

    我为在图像上添加包装器而创建的过滤器就是这样的

    // Remove P Tags from image
    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');
    
    // Wrap images with DIV
    function filter_images($content){
        return preg_replace('/<img (.*) \/>\s*/iU', '<span class="the-image"><img \1 /></span>', $content);
    }
    add_filter('the_content', 'filter_images');
    

    但我不知道如何进行第二项任务,即将DIV包装器添加到其余内容中。

0 个答案:

没有答案