I have text with a number of links. I want to reformat the text such that each url occurs after the name of its corresponding link and is wrapped in parenthesis - and all tags are removed (I'm writing this to CSV)
So for example,
<a href="http://test.com">TestWebsite1</a>
Becomes...
TestWebsite1 (http://test.com)
The approach I'm thinking is a bit tedious:
get index of each occurrence of "<a"
use regex to get all text following that up to next occurrence of ">"
find next occurrence of <
insert text at that index
str_replace "<a href=“ with "("
etc
I'm wondering if there's a better way...
You perhaps should look at markdown converter. and just use that format.
Please use a proper HTML parser:
$html = <<<HTML
<a href="http://test.com">TestWebsite1</a>
HTML;
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a[@href]') as $anchor) {
printf("%s (%s)
", $anchor->textContent, $anchor->getAttribute('href'));
}