doutan2111 2016-04-28 11:47
浏览 38
已采纳

从链接中提取文本

I'd like to extract onclick="printcoupon('292')" from this:

 [url=http://stackoverflow.com?foo=bar|onclick=printcoupon('292')]foobarbaz[/url]

The text inside the onclick can change dynamically but what I was thinking of was detecting if there is a word onclick inside this and if so grab the contents inside the double quotes (currently it says printcoupon('292') )

Basically though i need to grab either onclick=printcoupon('292') or just printcoupon('292').

Either way, this is unfamiliar territory.

    function bbcode($str) {
      // situation when str = [url=http://stackoverflow.com?foo=bar|onclick=printcoupon('292')]foobarbaz[/url]
      if(stristr($str,'onclick')){
        $parts=explode('|',$str);
        var_dump($parts);die();
        $str=preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/', '<a href="\1" style="text-decoration:none;color:#336699" onclick="PUT ONCLICK EXTRACTED CONTENT HERE">\2</a>', $str);

      // situation when str = [url=http://stackoverflow.com?foo=bar]foobarbaz[/url]
      } else {
        $str=preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/', '<a href="\1" style="text-decoration:none;color:#336699">\2</a>', $str);
      }
      return $str;
    }

So as you can see,

  • 写回答

1条回答 默认 最新

  • doupapin172773 2016-04-28 12:04
    关注

    If the substring you want ALWAYS begins with "onclick=" and ALWAYS ends with ")]", you can do this :

    <?php
    $s = "[url=http://stackoverflow.com?foo=bar|onclick=printcoupon('292')]foobarbaz[/url]";
    $onclick = strpos( $s,"onclick=" ); // POSITION OF "ONCLICK=".
    $closed_parenthesis = strpos( $s,")]" ); // POSITION OF ")]".
    echo substr( $s,$onclick,$closed_parenthesis - $onclick + 1 );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部