>  기사  >  백엔드 개발  >  在线等,急。PHP替换指定位置的字符串

在线等,急。PHP替换指定位置的字符串

WBOY
WBOY원래의
2016-06-23 13:41:18865검색


$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



老大。如果兼容PHP5.0的话  要怎么写呢。

#2 不就是吗?

#2 不就是吗?



哥哥 在帮我一下
$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';

红色部份的值不是固定的。。那这个正规要怎么弄呀。。。。。。。谢谢了

不固定?
那你描述的不是固定的吗?

不固定?
那你描述的不是固定的吗?


应该不固定吧, 如果固定,还要他有用吗?

$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);


\d\.\d{1,2} 这个确定 中间 的0.00, 别的格式自行调整

好吧,。,。是我错了。。。。我没有表答清楚。我开始以为很简单。结果我自已试了一下发现还不行

实际我要的效果是这样的

$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

噢,你的 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'; 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

噢,你的 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'; 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



啥也不说了。眼泪哇哇地。。。太感谢您了。。。@xuzuning 
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.