I want to encode all my links to base64
, but the regex pattern is not working as intended.
This is my code:
$html = 'ahsgdjagshjdgasjdgajgsd gjagdjhagsjhgda sgdhjagsjdgajhgdjad<a href="http://short.awsubs.co/be9Vk">Solidfiles</a> jashd sha7st7atsdgasgda sgahsfd ahgsfafd<a href=https://link.safelinkconverter.com/review.php?id=aHR0cDovL2JfdC5seS8ySDdMajh3&c=1&user=61942 rel=nofollow>Zippyshare</a>';
$text = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';
echo $result = preg_replace_callback($text, function($matches) {
return '<a href="'.base64_encode($matches[1]).'">'.$matches[2].'</a>';
}, $html);
I've read this thread that suggests using preg_replace_callback()
to encode the href
value:
I am going to provide an "unstable solution" since regex is not reliable. I've accommodated for single quoted, double quoted, and quote-less href attributes. But I must urge you to use a html parser. Your sample input is not very realistic to bother trying to write a DomDocument solution.
Unstable Code: (Demo) (Pattern Demo)
Output: