Heim > Artikel > Backend-Entwicklung > 在线等,急。PHP替换指定位置的字符串
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';
$b='38';
$c='85';
用PHP怎么做一替换。。。
就是说 当$a中 包含$b中的字数后。 把38,0.00,0.00,100 中的100替换成$c中的数字。
如果$b=‘37’
那么就把37,0.00,0.00,100 中的100替换成$c中的数字。
谢谢各位了
php5.3+ 通过
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,85|39,0.00,0.00,100
php5.5- 还可写作
echo preg_replace("/(^|\|$b,0.00,0.00,)100/e", "'$1'.'$c'", $a);
用新的子字符串替换原始字符串中被指定要替换的字符串
$str2="某某";
$str1="**";
$str=" 某某公司是一家以计算机软件技术为核心的高科技企业,多年来始终致力于行业管理软件开发、数字化出版物制作、计算机网络系统综合应用以及行业电子商务网站开发等领域,涉及生产、管理、控制、仓贮、物流、营销、服务等行业";
echo str_ireplace($str2,$str1,$str);
?>
php5.3+ 通过
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,85|39,0.00,0.00,100
#2 不就是吗?
#2 不就是吗?
不固定?
那你描述的不是固定的吗?
不固定?
那你描述的不是固定的吗?
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.01,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo "{$a}<br>";echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a).'<br>';echo preg_replace_callback("/(^|\|$b,\d\.\d{1,2},\d\.\d{1,2},)100/", function($m) use ($c){ return $m[1].$c;}, $a);
好吧,。,。是我错了。。。。我没有表答清楚。我开始以为很简单。结果我自已试了一下发现还不行
实际我要的效果是这样的
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';
$b='38';
$c='85';
就是说 当$a中 包含$b中的字数后。 把38,0.00,0.00,100 中的100取出来 然后用这个100 减去$c里面的数字。
最后等于这样 36,0.00,0.00,100|37,0.00,0.00,100| 38,0.00,0.00,15|39,0.00,0.00,100
然后入库、。。。。。。。其它我想要这个。。。把问题想简单了
求各位帮个忙吧
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace_callback("/(^|\|$b,0.00,0.00,)(100)/", function($m) use ($c){ return $m[1].($m[2]-$c);}, $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/(^|\|$b,0.00,0.00,)(100)/e", "'$1'.($2-$c)", $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/((?:^|\|$b),(?:[\d.]+,){2})(\d+)/e", "'$1'.($2-$c)", $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace_callback("/(^|\|$b,0.00,0.00,)(100)/", function($m) use ($c){ return $m[1].($m[2]-$c);}, $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/(^|\|$b,0.00,0.00,)(100)/e", "'$1'.($2-$c)", $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/((?:^|\|$b),(?:[\d.]+,){2})(\d+)/e", "'$1'.($2-$c)", $a);36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100