使用XML 相关扩展操作
https://www.php.net/manual/zh/refs.xml.php
示例代码
以处理图片地址为例子
$htmlContent = '<p>测试<img src='xxx.jpeg'></p>';
$htmlDom = new DOMDocument();
@$htmlDom->loadHTML($htmlContent);
$images = $htmlDom->getElementsByTagName('img');
//处理富文本中的图片
foreach ($images as $key => $image)
{
//获取img图片的src属性值
$src = $image->getAttribute('src');
//拼接成完整的url
$image->setAttribute('src', 'http://xxxx.com'.$src);
}
//获取body标签的内容
$body = $htmlDom->getElementsByTagName('body')->item(0);
//转换成html字符串
$content = $htmlDom->saveHTML($body);
//替换掉body标签
$content = str_replace(['<body>', '</body>'], '', $content);