从使用preg_replace创建的变量中删除字符 [英] Removing characters from a variable created using preg_replace

查看:278
本文介绍了从使用preg_replace创建的变量中删除字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试从preg_replace函数获取的URL末尾砍掉几个字符.但是,它似乎不起作用.我对在preg_replace中使用这些变量不熟悉(这只是我发现大部分"有效的功能).

So I'm trying to hack off a few characters at the end of a URL I'm getting from a preg_replace function. However it doesn't seem to be working. I'm not familiar with using these variables in preg_replace (it was just something I found that "mostly" worked).

这是我的尝试:

function addlink_replace($string) {
    $pattern = '/<ul(.*?)class="slides"(.*?)<img(.*?)src="(.*?)"(.*?)>(.*?)<\/ul>/is';
    $URL = substr($4, 0, -8);;

    $replacement = '<ul$1class="slides"$2<a rel=\'shadowbox\' href="'.$URL.'"><img$3src="$4"$5></a>$6</ul>';
    return preg_replace($pattern, $replacement, $string);
}
add_filter('the_content', 'addlink_replace', 9999);

基本上,我需要删除.jpg文件名的最后一位,所以我可以显示大图像而不是它生成的缩略图,但是"$ 4"似乎不想被操纵.

Basically I need to remove the last bit of my .jpg file name, so I can show the LARGE image rather than the THUMBNAIL it's generating, but the "$4" doesn't seem to want to be manipulated.

推荐答案

此答案基于您要在此问题中使用您已经指出,您需要匹配<ul>中的所有<li><img>标签,因此我已经写了一个更大的函数来做到这一点.

This answer is based off of what you're looking to accomplish in this question with the HTML structure of your other question. The regex that is posted in your question will not match anything other than the first set of <li> and <img> tags , and you've indicated that you need to match all <li> and <img> tags within a <ul> so I've written a larger function to do so.

它将包装<ul>slides内的<li>内的所有<img>标记,其中slides类带有<a>,其中源是图像的URL,而删除了-110x110字符串,同时将缩略图源保留在<img>标签中.

It will wrap all <img> tags that are inside of an <li> within a <ul> with the class of slides with an <a> with the source being the image's URL with the -110x110 string removed, while preserving the thumbnail source in the <img> tag.

function addlink_replace($string) {
    $new_ul_block = '';
    $ul_pattern = '/<ul(.*?)class="slides"(.*?)>(.*?)<\/ul>/is';
    $img_pattern = '/<li(.*?)>(.*?)<img(.*?)src="(.*?)"(.*?)>(.*?)<\/li>/is';

    preg_match($ul_pattern, $string, $ul_matches);

    if (!empty($ul_matches[3]))
    {
        preg_match_all($img_pattern, $ul_matches[3], $img_matches);

        if (!empty($img_matches[0]))
        {
            $new_ul_block .= "<ul{$ul_matches[1]}class=\"slides\"{$ul_matches[2]}>";

            foreach ($img_matches[0] as $id => $img)
            {
                $new_img = str_replace('-110x110', '', $img_matches[4][$id]);
                $new_ul_block .= "<li{$img_matches[1][$id]}>{$img_matches[2][$id]}<a href=\"{$new_img}\">";
                $new_ul_block .= "<img{$img_matches[3][$id]}src=\"{$img_matches[4][$id]}\"{$img_matches[5][$id]}></a>{$img_matches[6][$id]}</li>";
            }

            $new_ul_block .= "</ul>";
        }
    }

    if (!empty($new_ul_block))
    {
        $replace_pattern = '/<ul.*?class="slides".*?>.*?<\/ul>/is';
        return preg_replace($replace_pattern, $new_ul_block, $string);
    }
    else
    {
        return $string;
    }
}

<a>href属性相对于图像的更改是在行上专门进行的

The change of the <a>'s href attribute from what the image had is specifically done on the line

$new_img = str_replace('-110x110', '', $img_matches[2][$id]);

,如果您想对其进行修改.如果您需要从URL中删除除-110x110以外的任何内容,则可能需要将其从str_replace更改为preg_replace,或者如果您想从URL末尾删除特定数量的字符,则可以使用substr:

if you would like to modify it. If you need to remove anything other than -110x110 from the URL you may need to change it from str_replace to a preg_replace, or if you want to remove a specific number of characters from the end of the URL, you could use substr:

$new_img = substr($img_matches[2][$id], 0, -12);

-12是要从字符串末尾删除的字符数(它是负数,因为它从末尾开始).

Where -12 is the number of characters you want to remove from the end of the string (it's negative because it's starting at the end).

我已经在此处上发布了此功能的有效示例.

I've posted a working example of this function here.

您可能要考虑修改生成此代码块的源,而不是使用此正则表达式,因为如果HTML结构发生更改,将来可能很难维护此正则表达式.

You may want to consider modifying the source of what is generating this code block, rather than using this regex, as this regex may be hard to maintain in the future if the HTML structure changes.

这篇关于从使用preg_replace创建的变量中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆