dongxiane0395 2014-12-17 07:56
浏览 45

一个正则表达式,在Apache上工作正常,但在nginx上失败

I have a following regex expression which works well in apache but doesn't in nginx.

 preg_replace("/(src=\")(.*)([^>]*)(".$base_url_mod."\/)/",
  "data-$1$2$3", $content);

If I remove (".$base_url_mod."\/)/ it started to work but that's not what I want.

Full function:

function tt_type_filter ( $content ) {
    $post_type = get_post_type();

    if($post_type == 'page' || $post_type == 'post') {
        $tt_connector = get_option ( 'tt_connector' );
        $base_url = get_site_url();

        $base_url_arr = parse_url($base_url);
        $base_url_mod = $base_url_arr[scheme]. '\:\/\/' .$base_url_arr[host]. '\\' .$base_url_arr[path];

        if($tt_connector == $base_url) 
            return preg_replace("/(src=\")(.*)([^>]*)(".$base_url_mod."\/)/", "data-$1$2$3", $content);
        else 
            return preg_replace("/(src=\")(.*)([^>]*)(uploads\/)/", "data-$1$3", $content);
    }
}

Eg.(Purpose of this expression)

http://wp.mysite.net/wp4/wp-content/uploads/2012/10/someimage.jpg should be replaced with http://api.mydomain.com/images/domain/2012/10/someimage.jpg

  • 写回答

1条回答

  • donglie7778 2014-12-18 02:28
    关注

    I have managed to fix it at my own.

    The issue was with $base_url_mod. It was adding \/ at the end of the path when $base_url_arr[path]having empty/no value. So I'd to put a condition in the function and it started to work.

    Here is an updated function: (works fine on both, apache and nginx)

    function tt_type_filter ( $content ) {
        $post_type = get_post_type();
    
        if($post_type == 'page' || $post_type == 'post') {
            $tt_connector = get_option ( 'tt_connector' );
            $base_url = get_site_url();
    
            $base_url_arr = parse_url($base_url);
            if ($base_url_arr[path]) {
                $base_url_mod = $base_url_arr[scheme]. '\:\/\/' .$base_url_arr[host]. '\\' .$base_url_arr[path];
            } else {
                $base_url_mod = $base_url_arr[scheme]. '\:\/\/' .$base_url_arr[host];
            }
    
            if($tt_connector == $base_url)
                return preg_replace("/(src=\")(.*)([^>]*)(".$base_url_mod."\/)/", "data-$1$2$3", $content);
            else
                return preg_replace("/(src=\")(.*)([^>]*)(uploads\/)/", "data-$1$3", $content);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集