0

I can count the number of matching links with the preg_match_all() function like below, which checked if the inner text of the link is equal to the specified keyword.

// text contains two different links, whereby the inner 
// text of the first link is capitalized and the second link starts with a small letter.
   $string = "<p><a href=\"www.link1.com\" class=\"someClass\" title=\"lorem\">Lorem</a> dolor sit amet, consectetur
        adipiscing elit. In iaculis, libero aliquam lacinia feugiat, <a href=\"www.link2.com\" class=\"someClass\" title=\"lorem\">lorem</a>
        elit congue risus, sed sagittis turpis tortor eget orci. Integer lacinia quis nisi ac aliquet. Sed et convallis diam.</p>";


// count al matches by upper and lowercase sensitivity
preg_match_all('/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>lorem<\/a>/siU', $string, $match); 

Now my question is how I can make the regex so that it's also works for matches with a capital letter.

CodeWhisperer
  • 1,143
  • 2
  • 19
  • 39
  • 1
    Possible duplicate of [this question](http://stackoverflow.com/questions/12411037/how-do-i-make-this-preg-match-case-insensitive) – Michel Jan 27 '16 at 09:46

1 Answers1

1

Please use another approach instead, e.g. with an xpath:

$xml = simplexml_load_string($string);
$links= $xml->xpath("//a");
foreach ($links as $link)
    echo $link["href"];

See a demo on ideone.com. A solution with a regex would be:

~(?i)href=('|")(?<link>[^'"]+)\1(?i-)~
# case-insensitive
# look for href= literally
# look for a single/double quote and capture it in group 1
# match everything that is not a singel or double quote 1 or more times
# match the first captured group again
# and turn case sensitivity on again

A demo can be found on regex101.com, but better use the first approach.

Jan
  • 42,290
  • 8
  • 54
  • 79