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);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?