1). preg_match:正则匹配,返回:后面的字符串
$defaultpic="http://www.baidu.com";
preg_match("/[^:]+$/", $defaultpic, $matches);
$default_https='https:'.$matches[0];
$matches的运行结果为:[0=>'//www.baidu.com']
2). str_replace:字符串替换需要替换的字符
$content="<img src='/uditor/www/fans/upload/fhj9fwefaklooos.jpg'>";
if(strpos($content,'src="/ueditor')===false){
$photos=str_replace('\\','',$content); //用空的字符串替换反斜杠,如果没有反斜杠则不用替换
$photos=str_replace('src="/ueditor','src="http://www.baidu.com/ueditor',$photos);
$data->content_host=$photos;
}
*将返回的相对路径的图片更改为带域名的绝对路径
本文介绍了如何使用PHP的preg_match函数进行正则匹配,以获取URL中的特定部分,并展示了一个实例,同时讲解了如何利用str_replace函数将图片的相对路径转换为包含完整域名的绝对路径,这对于网站迁移或更新域名时非常实用。
712

被折叠的 条评论
为什么被折叠?



