php怎么替换大于5位数的数字为<img src="tar.php?=12345" width="709" height="29" /> php怎么替换大于5位数的数字为:比如:内容有123 , 1234 ,12345,但是只替换大于5位数的数字,也就是替换12345为<img src="tar.php?=12345" width="709" height="29" /> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $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" /> 先谢谢这位兄弟的回复,但是你这样只是把数字替换固定字符了,我要的是,只要是大于5位数的数字就输出为图片(如):内容有“1234电信移动12345”那么替换12345为“<img src="tar.php?=(这个里是内容里面大于5位的数字,也就是说内容是12345这就12345如果内容是456789这就是456789)" width="709" height="29" />” $string = preg_replace('^(.*)(\d{5,})(.*)$/','\\1<img src="tar\.php\?=\\2">\\3',$string); 继续求最佳答案谢谢回复者:自动替换内容大于5位数的数字为<img src="tar.php?=数字" width="709" height="29" /> $string="1234电信移动123451234"; $string = preg_replace("/^(.*)(12345)(.*)$/si","\\1<img src=\"tar\.php\?=\\2\">\\3",$string); echo $string;exit; 谢谢回复,这位兄弟,这样只是替换指定数字,要自动替换大于5位数的数字(大于5位数的数字包括123456789)继续求最佳答案谢谢回复者: 自动替换内容大于5位数的数字为 <img src="tar.php?=数字" width="709" height="29" /> 如果你字符串中只有一个大于5位的字符串的话就用:<?php$string='1234电信移动12345afsdf342342sdf ';$str =preg_replace('/^(.*)(\d{5,})(.*)$/','\\1<img src="tar.php?=\\2" width="709" height="29" />\\3',$string);?>如果你字符串里面有多个大于5位的数字字符串,都想替换的话就用:<?phppreg_match_all('/\d{5,}/',$string,$matches);foreach($matches as $single)$answer=str_replace($single,"<img src='tar.php?=$single' width='709' height='29' />",$string);?> 呵呵,上面那个一不小心写错了哈,呵呵该是这个<?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;?>这回对了,测试过的 谢谢回复,还是有错误!只要有重复的大于5位数的数字就会出现错误比如字符为“$string='1234电信移动12345afsdf342342sdf123456789'; ” 晕哦。走弯路~~<?php$string='1234电信移动12345afsdf12345342342sfdss12345vxcdf ';$str =preg_replace('/(\d{5,})/','<img src="tar.php?=\\1" width="29" height="29" />',$string);echo $str.'<br>';?>这下肯定对~ 谁能推荐一本php的书啊! php开发的纯WEB的视频会议网站怎么做到的 关于flex获取到的数据,如何能在页面循环显示?? 请问ie无法直接打开pdf 不知道js能实现这样的效果不,哪位见过类似的效果? smarty中的循环(只要10分了对不住还请大家帮忙) PHP $_SERVER['DOCUMENT_ROOT'] 问题 正则正则!!! 我想在本机上配个邮件服务器 ◇◆这一很简单的SQL语句有错吗?请大家帮助看看!!◆◇ 下载数据问题。 mysql_affected_rows ()运用错误的问题
echo preg_replace('/\d{5,}/', 'haha', $t);<img src="tar.php?=haha" width="709" height="29" />
自动替换内容大于5位数的数字为
<img src="tar.php?=数字" width="709" height="29" />
$string = preg_replace("/^(.*)(12345)(.*)$/si","\\1<img src=\"tar\.php\?=\\2\">\\3",$string);
echo $string;exit;
继续求最佳答案谢谢回复者:
自动替换内容大于5位数的数字为
<img src="tar.php?=数字" width="709" height="29" />
就用:
<?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);
?>
该是这个
<?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;
?>这回对了,测试过的
比如字符为“$string='1234电信移动12345afsdf342342sdf123456789'; ”
<?php
$string='1234电信移动12345afsdf12345342342sfdss12345vxcdf ';
$str =preg_replace('/(\d{5,})/','<img src="tar.php?=\\1" width="29" height="29" />',$string);
echo $str.'<br>';
?>
这下肯定对~