ホームページ >バックエンド開発 >PHPチュートリアル >オンラインで待機中です。緊急です。 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);
用新的子字符串替换原始字符串中被指定要替换的字符串
1ae8a14c791d5336baf45c44460b6340
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