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 #180379 is a reply to message #180373] Wed, 06 February 2013 14:58 Go to previous message
Jonathan N. Little is currently offline  Jonathan N. Little
Messages: 12
Registered: February 2011
Karma:
Junior Member
nawfer wrote:
> 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



If you are worried about leading and trailing whitespace in each element
then just add the 'trim' function


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
//trim whitespace first
return substr(trim($v), 0, -1) . '4';
}



--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
[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: Tue Nov 26 07:37:12 GMT 2024

Total time taken to generate the page: 0.05069 seconds