fork download
  1. <?php
  2.  
  3. $html = '
  4. <a href="http://w...content-available-to-author-only...e.net/index.php?p[some stuff to find]/1/">
  5. <a href=\'http://w...content-available-to-author-only...e.net/index.php?p[more stuff to find]/1/ \'>
  6. <a href = http://w...content-available-to-author-only...e.net/index.php?p[unquoted_stuff_to_find]/1/ />
  7. ';
  8.  
  9. $ref_txtstart = 'http://w...content-available-to-author-only...e.net/index.php?p';
  10. $ref_txtend = '/1/';
  11.  
  12. $regex =
  13. '~
  14. <a
  15. (?=\s)
  16. (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) href \s*=
  17. (?|
  18. (?>
  19. \s* ([\'"]) \s*
  20. ' . preg_quote($ref_txtstart) . ' ((?:(?!\g{-2}).)*) ' . preg_quote($ref_txtend) . '
  21. \s* \g{-2}
  22. )
  23. |
  24. (?>
  25. (?!\s*[\'"]) \s* ()
  26. ' . preg_quote($ref_txtstart) . ' ([^\s>]*) ' . preg_quote($ref_txtend) . '
  27. (?=\s|>)
  28. )
  29. )
  30. )
  31. \s+ (?:".*?"|\'.*?\'|[^>]*?)+
  32. >~xs
  33. ';
  34.  
  35. echo ("$regex\n");
  36. preg_match_all( $regex, $html, $matches, PREG_SET_ORDER );
  37. foreach ($matches as $val) {
  38. echo( "matched = $val[0]\ncore = $val[2]\n\n" );
  39. }
  40. ?>
  41.  
Success #stdin #stdout 0.02s 13232KB
stdin
Standard input is empty
stdout
~
<a 
  (?=\s) 
  (?= (?:[^>"']|"[^"]*"|'[^']*')*? (?<=\s) href \s*=
    (?|
        (?>
           \s* (['"]) \s*
           http\://www\.exemple\.net/index\.php\?p ((?:(?!\g{-2}).)*) /1/
           \s* \g{-2}
        )
      |
        (?> 
           (?!\s*['"]) \s* ()
           http\://www\.exemple\.net/index\.php\?p ([^\s>]*) /1/
           (?=\s|>)
        )
    )
  )
  \s+ (?:".*?"|'.*?'|[^>]*?)+ 
>~xs

matched = <a href="http://w...content-available-to-author-only...e.net/index.php?p[some stuff to find]/1/">
core    = [some stuff to find]

matched = <a href='http://w...content-available-to-author-only...e.net/index.php?p[more stuff to find]/1/ '>
core    = [more stuff to find]

matched = <a href = http://w...content-available-to-author-only...e.net/index.php?p[unquoted_stuff_to_find]/1/ />
core    = [unquoted_stuff_to_find]