regex - php preg_match_all href inner texts with upper and lower case sensitivity -
i can count number of matching links preg_match_all()
function below, checked if inner text of link equal specified keyword.
// text contains 2 different links, whereby inner // text of first link capitalized , second link starts 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 upper , lowercase sensitivity preg_match_all('/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>lorem<\/a>/siu', $string, $match);
now question how can make regex it's works matches capital letter.
please use approach instead, e.g. xpath:
$xml = simplexml_load_string($string); $links= $xml->xpath("//a"); foreach ($links $link) echo $link["href"];
see a demo on ideone.com. solution regex be:
~(?i)href=('|")(?<link>[^'"]+)\1(?i-)~ # case-insensitive # href= literally # single/double quote , capture in group 1 # match not singel or double quote 1 or more times # match first captured group again # , turn case sensitivity on again
a demo can found on regex101.com, better use first approach.
Comments
Post a Comment