修改字符串php中的数字 [英] modify number in string php

查看:87
本文介绍了修改字符串php中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi2all

i想要在逗号后搜索第一个号码,如果找到匹配,则用另一个号码替换第二个号码我指定它



my代码是



 $ value = '  1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0, 10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 10 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0'; 
$ resouces_array = explode(' ,',$ value);

foreach ($ resouces_array as $ key => $ value){
$ first_number = substr($ value,0,2);
$ second_number = explode(' ',$ value);

if($ first_number == 23){
$ second_number [1] = 50 ;
}

// $ first_parts = explode('',$ value) ;
$ string_valo = implode(' ',$ second_number);
$ after_spli = str_pad($ string_valo,6,' ,');
echo $ after_spli ;
}





i希望输出像那样

 $ value ='1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0 ,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 50 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0 ,0 0 0,16 1 0,0 0 0'; 





但我的代码产生了



 1 0 0,4 2 0,1 20 03 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0, 0 0 0,0 0 0,10 20 015 20 00 0 0,0 0 0,11 20 023 50 00 0 0,0 0 0,27 7 00 0 0,0 0 0,0 0 0,0 0 0, 0 0 0,16 1 00 0 0,





所以有什么问题,是否有其他简单的解决方案?

解决方案

value = ' 1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0 ,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 10 0 ,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0';


resouces_array = explode(' ,'

值);

foreach


hi2all
i want to search for first number after comma and if match found replace second number with another number i specify it

my code is

$value='1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 10 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0';
$resouces_array=explode(',', $value);

foreach ($resouces_array as $key=> $value) {
    $first_number  =substr($value,0,2);
    $second_number   = explode(' ', $value);

    if($first_number == 23){
         $second_number[1]= 50;
     }

    //$first_parts= explode(' ', $value);
$string_valo= implode(' ' ,$second_number);
     $after_spli=str_pad($string_valo, 6,',');
     echo $after_spli;
}



i want the output to be like that

$value='1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 50 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0';



but my code produce that

1 0 0,4 2 0,1 20 03 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 015 20 00 0 0,0 0 0,11 20 023 50 00 0 0,0 0 0,27 7 00 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 00 0 0,



so what is wrong and is there any simple solution other mine ??

解决方案

value='1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 10 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0';


resouces_array=explode(',',


value); foreach (


这篇关于修改字符串php中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆