就把.shtml之前的数字取出来,放到后面的$1的位置,调晕了,来请教大家
$contents = 'vod/464041.shtml';
$contents = preg_replace("vod\/([0-9])\.shtml","http://abc.def.com/vod/\$1.shtml",$contents);
var_dump($contents);

解决方案 »

  1.   


    $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)
    */
      

  2.   

    楼上的兄弟,请问为什么加了个参数就不行了呢?
    请看:$_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);
      

  3.   

    lz的错误在于[0-9]后面少个+$contents = 'vod/464041.shtml';
    $contents = preg_replace("/vod\/([0-9]+)\.shtml/","http://abc.def.com/vod/\$1.shtml",$contents);
    var_dump($contents);
      

  4.   


    $_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);还是加了参数就不行了
      

  5.   

    $_site."/vod\/([0-9]+)\.shtml/"
    你的正则错了,是 / 开始和结束(当然也可以使用 @# 之类似的)
    你现在是 site 这个开始了。打开错误提示就知了,是正则表达式错误了"/{$_site}vod\/([0-9]+)\.shtml/"
      

  6.   


    你把参数加到正则模式中?
    这样的话,你的原始字符串也要转义的。不过这样是无法匹配的,因为contents中不包含符合模式的串
      

  7.   


    $_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;貌似还是不行
      

  8.   


    当然不行。
    $_site = 'http://www.test.com/';中的特殊字符都要转义$site_for_reg = "http:\/\/www.test.com\/";不过已经说过了,你的contents种不包含能够匹配相应模式的串,所以结果是空的。