脚本处理
This commit is contained in:
@@ -27,29 +27,47 @@ class DomainCommand extends Command
|
|||||||
}
|
}
|
||||||
$item->content = processRichTextImagesEnhanced($item->content);
|
$item->content = processRichTextImagesEnhanced($item->content);
|
||||||
$item->save();
|
$item->save();
|
||||||
|
$output->writeln("处理了{$item->id} \n");
|
||||||
}
|
}
|
||||||
return self::SUCCESS;
|
return self::SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 增强版 - 为富文本内容中的图片URL添加域名前缀
|
* 增强版 - 处理富文本内容中的图片URL
|
||||||
*
|
*
|
||||||
* @param string $content 富文本HTML内容
|
* @param string $content 富文本HTML内容
|
||||||
* @param string $domain 域名(如: https://fdesk.hnxinyouhe.com)
|
* @param string $newDomain 新域名(如: https://fdesk.hnxinyouhe.com)
|
||||||
|
* @param string $oldDomain 旧域名(如: http://219.157.255.213:25326)
|
||||||
* @return string 处理后的富文本内容
|
* @return string 处理后的富文本内容
|
||||||
*/
|
*/
|
||||||
function processRichTextImagesEnhanced($content, $domain = 'https://fdesk.hnxinyouhe.com') {
|
function processRichTextImagesEnhanced($content, $newDomain = 'https://fdesk.hnxinyouhe.com', $oldDomain = 'http://219.157.255.213:25326') {
|
||||||
// 确保域名不以斜杠结尾
|
// 标准化域名格式(去除末尾斜杠)
|
||||||
$domain = rtrim($domain, '/');
|
$newDomain = rtrim($newDomain, '/');
|
||||||
|
$oldDomain = rtrim($oldDomain, '/');
|
||||||
|
|
||||||
// 要处理的属性列表
|
// 要处理的属性列表
|
||||||
$attributes = ['src', 'data-href', 'href', 'data-src', 'data-original'];
|
$attributes = ['src', 'data-href', 'href', 'data-src', 'data-original'];
|
||||||
|
|
||||||
foreach ($attributes as $attr) {
|
foreach ($attributes as $attr) {
|
||||||
|
// 情况1:替换旧域名为新域名
|
||||||
|
$content = preg_replace(
|
||||||
|
'/('.$attr.'=["\'])'.preg_quote($oldDomain, '/').'(\/uploads\/)/i',
|
||||||
|
'$1' . $newDomain . '$2',
|
||||||
|
$content
|
||||||
|
);
|
||||||
|
|
||||||
|
// 情况2:给无域名的路径添加新域名
|
||||||
$content = preg_replace(
|
$content = preg_replace(
|
||||||
'/('.$attr.'=["\'])(\/uploads\/)/i',
|
'/('.$attr.'=["\'])(\/uploads\/)/i',
|
||||||
'$1' . $domain . '$2',
|
'$1' . $newDomain . '$2',
|
||||||
|
$content
|
||||||
|
);
|
||||||
|
|
||||||
|
// 情况3:处理可能存在的其他域名(可选)
|
||||||
|
$content = preg_replace(
|
||||||
|
'/('.$attr.'=["\'])(https?:\/\/[^\/]+)(\/uploads\/)/i',
|
||||||
|
'$1' . $newDomain . '$3',
|
||||||
$content
|
$content
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user