php怎么替换大于5位数的数字为:
比如:内容有123 , 1234 ,12345,但是只替换大于5位数的数字,也就是替换12345为
<img src="tar.php?=12345"  width="709" height="29" />

解决方案 »

  1.   

    $t = '<img src="tar.php?=12345"  width="709" height="29" />';
    echo preg_replace('/\d{5,}/', 'haha', $t);<img src="tar.php?=haha"  width="709" height="29" />
      

  2.   

    先谢谢这位兄弟的回复,但是你这样只是把数字替换固定字符了,我要的是,只要是大于5位数的数字就输出为图片(如):内容有“1234电信移动12345”那么替换12345为“<img src="tar.php?=(这个里是内容里面大于5位的数字,也就是说内容是12345这就12345如果内容是456789这就是456789)"  width="709" height="29" />”
      

  3.   

    $string = preg_replace('^(.*)(\d{5,})(.*)$/','\\1<img src="tar\.php\?=\\2">\\3',$string);
      

  4.   

    继续求最佳答案谢谢回复者:
    自动替换内容大于5位数的数字为
    <img src="tar.php?=数字"  width="709" height="29" />
      

  5.   

    $string="1234电信移动123451234";
    $string = preg_replace("/^(.*)(12345)(.*)$/si","\\1<img src=\"tar\.php\?=\\2\">\\3",$string);

    echo $string;exit;
      

  6.   

    谢谢回复,这位兄弟,这样只是替换指定数字,要自动替换大于5位数的数字(大于5位数的数字包括123456789)
    继续求最佳答案谢谢回复者: 
    自动替换内容大于5位数的数字为 
    <img src="tar.php?=数字"  width="709" height="29" />
      

  7.   

    如果你字符串中只有一个大于5位的字符串的话
    就用:
    <?php
    $string='1234电信移动12345afsdf342342sdf ';
    $str =preg_replace('/^(.*)(\d{5,})(.*)$/','\\1<img src="tar.php?=\\2"  width="709" height="29" />\\3',$string);
    ?>
    如果你字符串里面有多个大于5位的数字字符串,都想替换的话
    就用:
    <?php
    preg_match_all('/\d{5,}/',$string,$matches);
    foreach($matches as $single)
    $answer=str_replace($single,"<img src='tar.php?=$single' width='709' height='29' />",$string);
    ?>
      

  8.   

    呵呵,上面那个一不小心写错了哈,呵呵
    该是这个
    <?php
    $string='1234电信移动12345afsdf342342sdf';
    preg_match_all('/\d{5,}/',$string,$matches);
    foreach($matches[0] as $single)
    {
    $string=str_replace($single,"<img src='tar.php?=$single' width='709' height='29' />",$string);
    }
    echo $string;
    ?>这回对了,测试过的
      

  9.   

    谢谢回复,还是有错误!只要有重复的大于5位数的数字就会出现错误
    比如字符为“$string='1234电信移动12345afsdf342342sdf123456789'; ”
      

  10.   

    晕哦。走弯路~~
    <?php
    $string='1234电信移动12345afsdf12345342342sfdss12345vxcdf ';
    $str =preg_replace('/(\d{5,})/','<img src="tar.php?=\\1"  width="29" height="29" />',$string);
    echo $str.'<br>';
    ?>
    这下肯定对~