Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Php Wordpress函数-如何使用替换字符串函数。添加一些代码_Php_Wordpress - Fatal编程技术网

Php Wordpress函数-如何使用替换字符串函数。添加一些代码

Php Wordpress函数-如何使用替换字符串函数。添加一些代码,php,wordpress,Php,Wordpress,我之前使用了一些我发现的代码,在Wordpress中将“rel=shadowbox”添加到我的链接中(这就像一个符咒) /* 插件名称:添加阴影盒 */ 定义(“图像文件类型”,“bmp | gif | jpeg | jpg | png)”,true); 函数addlightboxrel\u replace($string){ $pattern='/'; 返回preg_replace($pattern,$replacement,$string); } 添加过滤器(“内容”、“添加链接”和“替换”

我之前使用了一些我发现的代码,在Wordpress中将“rel=shadowbox”添加到我的链接中(这就像一个符咒)

/*
插件名称:添加阴影盒
*/
定义(“图像文件类型”,“bmp | gif | jpeg | jpg | png)”,true);
函数addlightboxrel\u replace($string){
$pattern='/';
返回preg_replace($pattern,$replacement,$string);
}
添加过滤器(“内容”、“添加链接”和“替换”);
这是插件正在吐出的当前代码:

<ul class="slides" style="width: 600%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
    <li style="float: left; display: block; width: 100px;">
        <img width="110" height="110" class="slider-247 slide-243" alt="" src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1-110x110.jpg" draggable="false">
    </li>
</ul>
我想在图像周围包装一个“a”标记,使用a标记的src的当前图像URL。(如果可能,我需要删除jpg末尾的“-110x100”位

<ul class="slides" style="width: 600%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
    <li style="float: left; display: block; width: 100px;">
        <a src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1.jpg">
            <img width="110" height="110" class="slider-247 slide-243" alt="" src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1-110x110.jpg" draggable="false">
        </a>
    </li>
</ul>

  • 您拥有的正则表达式无法正确匹配,它缺少一些内容。我对其进行了一些更改和简化,因此如果无序列表的结构不是每次都相同,您可能需要修改它。以下是函数中更新的正则表达式:

    function addlink_replace($string) {
        $pattern = '/<ul(.*?)class="slides"(.*?)<img(.*?)src="(.*?)"(.*?)>(.*?)<\/ul>/is';
        $replacement = '<ul$1class="slides"$2<a href="$4"><img$3src="$4"$5></a>$6</ul>';
        return preg_replace($pattern, $replacement, $string);
    }
    
    函数addlink\u replace($string){
    
    $pattern='/Wow我很欣赏这里的深入回答和解释。不过,它似乎还没有添加代码,但我会看看regex101的东西,看看我是否能稍微处理一下。而且列表的结构总是一样的
    ,但这些标记中的某些属性可能会更改…这就是问题所在吗?这应该包括更改和添加属性(除非
    class=“slides”
    更改)。如果HTML结构与提供的示例相同,则正则表达式应该可以工作;此筛选器可能运行得太快。您可以稍后通过将筛选器调用更改为
    add\u filter('The\u content','addlink\u replace',9999)来设置优先级
    您试图将此应用于链接页面上的哪个元素?是大图像下面的三个叉车小图像吗?是的,这三个图像!class=“slides”从未更改,因此您添加的“.9999”完全起作用。图像现在正在加载到阴影框中(一旦我添加了rel属性,我的另一个正则表达式就不再有效了)。我只需要想办法现在加载非缩略图版本。我可能只需要将URL存储在一个变量中,去掉最后8个字符,然后使用它链接。非常感谢,你是一个救命恩人!如果有办法保存“$4”在“href”中作为变量,然后在替换代码中吐出新变量?
    <ul class="slides" style="width: 600%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
        <li style="float: left; display: block; width: 100px;">
            <a src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1.jpg">
                <img width="110" height="110" class="slider-247 slide-243" alt="" src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1-110x110.jpg" draggable="false">
            </a>
        </li>
    </ul>
    
    function addlink_replace($string) {
        $pattern = '/<ul(.*?)class="slides"(.*?)<img(.*?)src="(.*?)"(.*?)>(.*?)<\/ul>/is';
        $replacement = '<ul$1class="slides"$2<a href="$4"><img$3src="$4"$5></a>$6</ul>';
        return preg_replace($pattern, $replacement, $string);
    }