0

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,

slicks1
  • 349
  • 1
  • 13
  • What is used to output the HTML code in the first place? When you output it can you not just do whatever you need to do with the value then? – Mike Apr 28 '16 at 19:49
  • @JoseManuelAbarcaRodríguez Close. I can either grab `onclick=printcoupon('292')` or just `printcoupon('292')`. Not > though – slicks1 Apr 28 '16 at 19:51
  • 1
    Possible duplicate of [How to extract img src, title and alt from html using php?](http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php) – Mike Apr 28 '16 at 19:51
  • @JoseManuelAbarcaRodríguez My question was incorrect. Please see now – slicks1 Apr 28 '16 at 19:52
  • @JoseManuelAbarcaRodríguez K – slicks1 Apr 28 '16 at 19:53
  • @JoseManuelAbarcaRodríguez Code up. Yeh I'm looking at that now but did you see my code updates? I'm not actually using vanilla html and I wouldn't know im only able to scan for onclick – slicks1 Apr 28 '16 at 20:00

1 Answers1

0

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