Search code examples
phpregexencodinghrefpreg-replace-callback

How to encode all URL from String using Preg_Replace


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:

php how to do base64encode while doing preg_replace


Solution

  • 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)

    $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>';
    
    echo preg_replace_callback('~href=[\'"]?([^\s\'"]+)[\'"]?(.*?)>(.*?)</a>~', function($m) {
        var_export($m);
            return "<a href=\"" . base64_encode($m[1]) . "\"{$m[2]}>{$m[3]}</a>";
        }, $html);
    

    Output:

    ahsgdjagshjdgasjdgajgsd gjagdjhagsjhgda sgdhjagsjdgajhgdjad<a <a href="aHR0cDovL3Nob3J0LmF3c3Vicy5jby9iZTlWaw==">Solidfiles</a> jashd sha7st7atsdgasgda sgahsfd ahgsfafd<a <a href="aHR0cHM6Ly9saW5rLnNhZmVsaW5rY29udmVydGVyLmNvbS9yZXZpZXcucGhwP2lkPWFIUjBjRG92TDJKZmRDNXNlUzh5U0RkTWFqaDMmYz0xJnVzZXI9NjE5NDI=" rel=nofollow>Zippyshare</a>