Last active
November 19, 2018 17:53
-
-
Save rajvanshipradeep15/2856438f27b6cac20f1b967388aabd21 to your computer and use it in GitHub Desktop.
Get text value from string containing anchor tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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