简体   繁体   English

从链接中提取文本

[英]Extract text from link

I'd like to extract onclick="printcoupon('292')" from this: 我想从中提取onclick="printcoupon('292')"

 [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') ) onclick中的文本可以动态更改,但是我当时想的是检测其中是否有onclick单词,如果是,请抓住双引号中的内容(当前显示为printcoupon('292')

Basically though i need to grab either onclick=printcoupon('292') or just printcoupon('292') . 基本上,尽管我需要抓住onclick=printcoupon('292')或只是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, 如您所见,

If the substring you want ALWAYS begins with "onclick=" and ALWAYS ends with ")]", you can do this : 如果您想要的子字符串始终以“ onclick =“开头,而始终以“)]结尾,则可以执行以下操作:

<?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 );
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM