fork(1) download
  1. <?php
  2.  
  3.  
  4. // SET TEXT TO BE USED
  5. $string = 'some text some text <b>some text</b> some text <a href=http://sometextsometext<b>sometext</b>sometextsometext>text</a> some text some text. And We Have A <a href=http://google.com>Google</a> Link';
  6.  
  7.  
  8. // USE A CALLBACK FUNTION TO SCAN THROUGH LINKS
  9. $string = preg_replace_callback('~<a.*?</a>~', 'remove_crap_from_links', $string);
  10.  
  11. print $string;
  12.  
  13.  
  14. // THIS IS THE CALLBACK FUNCTION ... EACH LINK IS STORED AS '$m'
  15. function remove_crap_from_links($m) {
  16.  
  17. // PULL OUT THE PART OF THE LINK BEFORE THE CLOSING LINK BRACKET
  18. // (USE A NEGATIVE LOOKAHEAD TO MAKE SURE THAT IT CAN'T HAVE ANY OPENING/CLOSING HTML BRACKETS IN THERE
  19. if (preg_match('~<a(.*?)>(?:[^<>]*?)</a>~i', $m[0], $url_matches)) {
  20.  
  21. // RUN A PHP strip_tags FUNCTION TO PULL OUT ANY HTML TAGS FOUND IN THE LINK BODY
  22. $stripped_url = strip_tags($url_matches[1]);
  23.  
  24. // REBUILD THE URL, USING THE $stripped_url IN PLACE OF WHAT WAS ALREADY THERE
  25. $clean_url = preg_replace('~(<a)(.*?)(>(?:[^<>]*?)</a>)~', '$1'.$stripped_url.'$3', $m[0]);
  26.  
  27. }
  28.  
  29.  
  30. return $clean_url;
  31.  
  32.  
  33. }
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
some text some text <b>some text</b> some text <a href=http://sometextsometextsometextsometextsometext>text</a> some text some text.  And We Have A <a href=http://google.com>Google</a> Link