I already have a function that retrieves the href
attribute from all of the a
tags on a given page of markup. However, I would also like to retrieve other attributes, namely the title
attribute.
I have a feeling it's a simple modification of the regular expression that I'm already using, but my only concern is the order of appearance in the markup. If I have a link with this code:
<a href="somepage.html" title="My Page">link text</a>
I want it to be parsed the same and not cause any errors even if it appears like this:
<a title="My Page" href="somepage.html">link text</a>
Here is my processing function:
function getLinks($src) {
if(preg_match_all('/<a\s+href=["\']([^"\']+)["\']/i', $src, $links, PREG_PATTERN_ORDER))
return array_unique($links[1]);
return false;
}
Would I have to use another regex all together, or would it be possible to modify this one so that the title
attribute is stored in the same array of returned data as the href
attribute?