使用 php 更改所有出现的 href 值

Alter all occurence of href values with php

我想在提交到数据库之前更改 href 标签的所有值。

假设我有一个段落,里面有一些锚标签,我想更改所有锚标签的值。

我的目标是对 url 进行 base64 编码,然后将前缀附加到 url 如果网址是

http://google.com

提交前想改成 http://exmaple.com/visit/base64encodeedurl

您可以使用preg_replace_callback function and base64_encode函数

$string = 'Your string to save with http://goo.gl urls ...';
$fixedString = preg_replace_callback(
    '#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,18}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si',
    function($matches) {
        return 'http://exmaple.com/visit/' . base64_encode($matches[0]); 
    },
    $string
);

这是一个活生生的例子https://3v4l.org/TUktY