duancutan4770 2014-01-06 19:23
浏览 41
已采纳

在正则表达式上前瞻

I am looking to have all my bbcode images that have a link attached to them be parsed before the normal [img] tag parsing is done in my code, this is because my normal [img] tags if they have no link get turned into a lightbox.

    $find = array(
    "/\[url=(.+?)\](?=\[img\](.+?)\[\/img\])\[\/url\]/is"
    );

    $replace = array(
    "<a href=\"$1\" target=\"_blank\"><img src=\"$2\" class=\"bbcodeimage img-polaroid\" alt=\"[img]\" /></a>"
    );

    $body = preg_replace($find, $replace, $body);

Totally wrong since it doesn't actually do anything it seems I am completely confused on the whole lookahead thing to capture [img] tags that have an [url] before it and replace accordingly.

  • 写回答

1条回答 默认 最新

  • dongzhuo3376 2014-01-06 19:47
    关注

    I my opinion you don't need lookahead at all. Try this:

    $find = array(
        '~\[url=([^]]+)]\[img]([^[]+)\[/img]\[/url]~i'
    );
    
    $replace = array(
        '<a href="$1" target="_blank"><img src="$2" class="bbcodeimage img-polaroid" alt="[img]" /></a>'
    );
    

    Explanations:

    First at all, I have changed the pattern delimiter to ~, the goal of this change is to avoid to escape all literal / in the pattern. Literals ] don't need to be escaped outside a character class or inside a character class if (and only if) it is the first character.

    A lookahead is not useful in this situation because a lookahead is only a check and matches nothing. Example a(?=bc) will find a a followed by bc but will only match the a. It is why lookaheads and lookbehinds are also called "zero width assertions".

    pattern details:

    ~           # delimiter
    \[url=      # literal: [url=
    (           # open the first capturing group
        [^]]+   # all characters except ] (one or more times)
    )           # close the first capturing group
    ]           # literal: ]
    \[img]      # literal: [img]
    (           # open the second capturing group
        [^[]+   # all characters except [ (one or more times)
    )           # close the second capturing group
    \[/img]     # literal: [/img]
    \[/url]     # literal: [/url]
    ~i          # delimiter and i modifier
    

    Note that I have choosen to use single quotes for the replacement string to avoid to escape all double quotes of the string (and because there is no reason to use double quotes, no variables, no or \t etc.).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化