Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajvanshipradeep15/2856438f27b6cac20f1b967388aabd21 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/2856438f27b6cac20f1b967388aabd21 to your computer and use it in GitHub Desktop.
Get text value from string containing anchor tag
var myrRegexp = /<a[^>]*>(.*?)<\/a>/i,
subjectString = '<a blah blah>Click Here</a>',
match = myrRegexp.exec(subjectString);
if (match != null && match.length > 1) {
return match[1];
} else {
return = "";
}
// PHP
$str = 'My long <a href="http://example.com/abc" rel="link">string</a> has any
<a href="/local/path" title="with attributes">number</a> of
<a href="#anchor" data-attr="lots">links</a>.';
$dom = new DomDocument();
$dom->loadHTML($str);
$output = array();
foreach ($dom->getElementsByTagName('a') as $item) {
$output[] = array (
'str' => $dom->saveHTML($item),
'href' => $item->getAttribute('href'),
'anchorText' => $item->nodeValue
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment