I have database with many news items and I have relative urls like this
/index.php?news etc...
I need to convert these urls to absolute, like this:
http(or - s)//example.com/index.php?news etc..
Also I have this code and it works
$regex = '~<a([^>]*)href=["\']([^"\']*)["\']([^>]*)>~';
$replace = '<a$1href="'. $absolute_url .'/$2"$3>';
$data->introtext = preg_replace($regex, $replace, $news->introtext);
where $news->introtext
is my text with many links eg:
<a href="/index.php">some text</a> some text here <a href="https://facebook.com/etc">
code works like this:
<a href="http://example.com/index.php">some text</a> some text here <a href="http://example.com/https://facebook.com/etc">
and I don't want to add http://example.com/ if link is like https://facebook.com/etc
Found good way:
$data->introtext = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#", '$1http://example.com/$2$3', $news->introtext);
if urls not exits then add url
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.