doutan2111 2016-04-28 19: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 20: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 );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错