就把.shtml之前的数字取出来,放到后面的$1的位置,调晕了,来请教大家
$contents = 'vod/464041.shtml';
$contents = preg_replace("vod\/([0-9])\.shtml","http://abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);
$contents = 'vod/464041.shtml';
$contents = preg_replace("vod\/([0-9])\.shtml","http://abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);
$contents = 'vod/464041.shtml';
$contents = preg_replace("/vod\/(\d+)\.shtml/","http://abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);
/**
输出结果:
string 'http://abc.def.com/vod/464041.shtml' (length=35)
*/
请看:$_site = 'http://www.test.com/';
$contents = 'vod/464041.shtml';
$contents = preg_replace($_site."/vod\/(\d+)\.shtml/","http://".$_site.".abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);
$contents = preg_replace("/vod\/([0-9]+)\.shtml/","http://abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);
$_site = 'http://www.test.com/';
$contents = 'vod/464041.shtml';
$contents = preg_replace($_site."/vod\/([0-9]+)\.shtml/","http://".$_site."abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);还是加了参数就不行了
你的正则错了,是 / 开始和结束(当然也可以使用 @# 之类似的)
你现在是 site 这个开始了。打开错误提示就知了,是正则表达式错误了"/{$_site}vod\/([0-9]+)\.shtml/"
你把参数加到正则模式中?
这样的话,你的原始字符串也要转义的。不过这样是无法匹配的,因为contents中不包含符合模式的串
$_site = 'http://www.test.com/';
$contents = 'vod/464041.shtml';
$contents = preg_replace("/{$_site}vod\/([0-9]+)\.shtml/","http://abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);
exit;貌似还是不行
当然不行。
$_site = 'http://www.test.com/';中的特殊字符都要转义$site_for_reg = "http:\/\/www.test.com\/";不过已经说过了,你的contents种不包含能够匹配相应模式的串,所以结果是空的。