就把.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);
解决方案 »
- 求一mysql 语句
- asp转换php
- php菜鸟寻求指导:如何从零开始?
- 不同系统不同数据库数据一致
- PHP怎样模拟一个POST来提交数据
- Mysql 错误 Illegal mix of collations.....
- 我的电脑打开网页时,状态栏不是提示的网址如:http://www.aa.com/happy.php,而是提示的happy.php在www.aa.com的快捷方式,很不爽,如何
- 大家好,多选表单选中,删除的时候,怎么删除传过来的id值啊?
- 如何结合HTML用PHP写一个问答题程序(高分)
- 对于高并发的抢购功能,有哪些方案能解决商品超卖问题?急
- php保存临时文件
- 求一个字符串处理
$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种不包含能够匹配相应模式的串,所以结果是空的。