orderBy('id', 'desc')->get(); foreach ($list as $item) { if (!$item->content) { continue; } $item->content = processRichTextImagesEnhanced($item->content); $item->save(); $output->writeln("处理了{$item->id} \n"); } return self::SUCCESS; } } /** * 增强版 - 处理富文本内容中的图片URL * * @param string $content 富文本HTML内容 * @param string $newDomain 新域名(如: https://fdesk.hnxinyouhe.com) * @param string $oldDomain 旧域名(如: http://219.157.255.213:25326) * @return string 处理后的富文本内容 */ function processRichTextImagesEnhanced($content, $newDomain = 'https://x-server.hnxinyouhe.com', $oldDomain = 'http://219.157.255.213:25326') { // 标准化域名格式(去除末尾斜杠) $newDomain = rtrim($newDomain, '/'); $oldDomain = rtrim($oldDomain, '/'); // 要处理的属性列表 $attributes = ['src', 'data-href', 'href', 'data-src', 'data-original']; foreach ($attributes as $attr) { // 情况1:替换旧域名为新域名 $content = preg_replace( '/('.$attr.'=["\'])'.preg_quote($oldDomain, '/').'(\/uploads\/)/i', '$1' . $newDomain . '$2', $content ); // 情况2:给无域名的路径添加新域名 $content = preg_replace( '/('.$attr.'=["\'])(\/uploads\/)/i', '$1' . $newDomain . '$2', $content ); // 情况3:处理可能存在的其他域名(可选) $content = preg_replace( '/('.$attr.'=["\'])(https?:\/\/[^\/]+)(\/uploads\/)/i', '$1' . $newDomain . '$3', $content ); } return $content; }