I am struggling converting html links into text keeping same html structure.
I need to covert this html page part
<div>
<p>text text bla blah</p>
<p><a href="https://google.com" rel="nofollow" target="_blank" title="google">Cool website</a></p>
<p><a href="https://google.com" rel="nofollow" target="_blank" title="google">Cool website</a></p>
</div>
into this
<div>
<p>text text bla blah</p>
<p>Cool website https://google.com</p>
<p>Cool website https://google.com</p>
</div>
I found a nice script PHP regex: How to convert HTML string with links into plain text that shows URL after text in brackets which collects html links and converts them into plain text and that is part of job.
this is what i have so far:
$htmlString = '
<div>
<p>text text bla blah</p>
<p><a href="https://google.com" rel="nofollow" target="_blank" title="google">Cool website</a></p>
<p><a href="https://google.com" rel="nofollow" target="_blank" title="google">Cool website</a></p>
</div>
';
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($htmlString);
$xpath = new DOMXPath($dom);
$links = [];
$linksAsString = '';
foreach ($xpath->query('//a') as $linkElement)
{
$link = [
'href' => $linkElement->getAttribute('href'),
'text' => $linkElement->textContent
];
$links[] = $link;
$linksAsString .= $link['text'] . " {$link['href']}<br/>";
}
libxml_clear_errors();
echo $linksAsString;
current code only outputs converted links:
Cool website https://google.com
Cool website https://google.com
I would appreciate some help.