FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » change a string where have number after a word
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: change a string where have number after a word [message #180373 is a reply to message #180363] Wed, 06 February 2013 10:47 Go to previous messageGo to previous message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma:
Member
Il Tue, 05 Feb 2013 17:14:38 -0500, Jonathan N. Little ha scritto:

> nawfer wrote:
>>> Couple of questions first.
>>>
>>> 1) From your example it looks like the comma separated values. it this
>>> the case were every value ends with a number?
>>
>>
>> every value ends with one space and a number
>> that can also be so I can have this type of words
>>
>> aaa 4,
>> aafagagsg 4,
>> afgsgfs 2,
>> a affs 4,
>> aa adfdf 2,
>> afsf ercrrt s 4
>>
>>
>> for the oteher question answer after
>>
>
> Then the simplest is what I offered with function always_single_digit()
> where you just chop off the last character and then replace with '4'.
>
> function always_single_digit($v){
> // If always each entry always has a single digit number
> // you can simply chop off the last as number 4
> return substr($v, 0, -1) . '4';
> }
>
> //your sample
> $str="aaa 4,aafagagsg 4,afgsgfs 2,a affs 4,aa adfdf 2 afsf ercrrt s 4";
> //split into array on ','
> $arry=explode(',', $str);
> //use array_map to use callback in each element to replace with 4
> print implode(',', array_map("always_single_digit", $arry));


I must to do this


$string="tested-x 4, mc dnd's 4, passenger 2, arrived 2, a.jhjhj.rts
4, te_ry4e_ll 4, pas44nger 2";

1st steps
$arr=explode(', ', $string);

and have
1---
Array ( [0] => tested-x 4
[1] => mc dnd's 4
[2] => passenger 2
[3] => arrived 2
[4] => a.jhjhj.rts 4
[5] => te_ry4e_12 4
[6] => pas44nger 2 )


now I must remove all elements where last two words aren't space4
[0] => tested-x 4 ok remain in the array
[3] => arrived 2 remove from array

and have this
2---
Array ( [0] => tested-x 4
[1] => mc dnd's 4


[4] => a.jhjhj.rts 4
[5] => te_ry4e_12 4
)


other step is remove space4 for to have this array
3---
Array ( [0] => tested-x
[1] => mc dnd's
[2] => a.jhjhj.rts
[3] => te_ry4e_12
)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: script php with <img scr...../>
Next Topic: classes in PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 07:33:14 GMT 2024

Total time taken to generate the page: 0.04384 seconds