diff --git a/app/command/DomainCommand.php b/app/command/DomainCommand.php new file mode 100644 index 0000000..61e0777 --- /dev/null +++ b/app/command/DomainCommand.php @@ -0,0 +1,58 @@ +orderBy('id', 'desc')->get(); + + foreach ($list as $item) { + if (!$item->content) { + continue; + } + $item->content = processRichTextImagesEnhanced($item->content); + $item->save(); + } + return self::SUCCESS; + } +} + +/** + * 增强版 - 为富文本内容中的图片URL添加域名前缀 + * + * @param string $content 富文本HTML内容 + * @param string $domain 域名(如: https://fdesk.hnxinyouhe.com) + * @return string 处理后的富文本内容 + */ +function processRichTextImagesEnhanced($content, $domain = 'https://fdesk.hnxinyouhe.com') { + // 确保域名不以斜杠结尾 + $domain = rtrim($domain, '/'); + + // 要处理的属性列表 + $attributes = ['src', 'data-href', 'href', 'data-src', 'data-original']; + + foreach ($attributes as $attr) { + $content = preg_replace( + '/('.$attr.'=["\'])(\/uploads\/)/i', + '$1' . $domain . '$2', + $content + ); + } + + return $content; +} \ No newline at end of file diff --git a/app/command/TbCommand.php b/app/command/TbCommand.php index 3618b40..d60482f 100644 --- a/app/command/TbCommand.php +++ b/app/command/TbCommand.php @@ -25,25 +25,4 @@ class TbCommand extends Command } return self::SUCCESS; } -} - - -function replaceRichTextUrls($input) { - // 匹配旧域名及其引号包裹情况(包含http/https协议) - $pattern = '#(https?://server\.xyh\.hnzhwlkj\.club|["\']https?://server\.xyh\.hnzhwlkj\.club)(/[^\s"\'<>]*)?#iu'; - - $replacement = function ($matches) { - $originalQuote = ''; - $path = $matches[2] ?? ''; - - // 检测是否以引号开头 - if (isset($matches[0][0]) && in_array($matches[0][0], ['"', "'"])) { - $originalQuote = $matches[0][0]; - } - - // 构建新URL(强制使用http协议) - return $originalQuote . 'http://219.157.255.213:25326' . $path; - }; - - return preg_replace_callback($pattern, $replacement, $input); } \ No newline at end of file