Friday, April 13, 2018

Laravel / PHP – Convert plain links in text to nice links with page title - development

Laravel / PHP – Convert plain links in text to nice links with page title

I’ve searched a lot but couldn’t really find any relevant discussion. Here’s the case at my hand –

When a user writes a blog post, they’ll simply add plain links in the text. Viz. https://somedomain.com . Now my editor can convert these links to actual html links viz. <a href="https://somedomain.com>https://somedomain.com</a>.

Now I want to extract all such links in the body and convert them into links with page titles. That is, the link should get converted to <a href="https://somedomain.com>Page Title</a>

I’ve been able to extract the URLs, but the problem is that I can’t find a way to fetch the page title.

$dom->loadHTML($request->body);
    $urls = $dom->getElementsByTagName('a');
    foreach($urls as $url) {
        $href = $url->getAttribute('href');
        // How do we get the title of this href, now? 
    }

PS: Would really appreciate if you could suggest a way to do this – or maybe refer a package that does this. I’ve found a package called ‘ESSENCE’ but that only works for media files.



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-php-convert-plain-links-in-text-to-nice-links-with-page-title/
via Lzo Media

No comments:

Post a Comment