Cómo codificar todas las URL de String usando Preg_Replace
Quiero codificar todos mis enlaces abase64
, pero el patrón regex no funciona según lo previsto.
Este es mi código:
$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);
He leído este hilo que sugiere usarpreg_replace_callback()
para codificar elhref
valor: