Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8313

Re: change a string where have number after a word

From "Jonathan N. Little" <lws4art@gmail.com>
Newsgroups comp.lang.php, alt.comp.lang.php, alt.php
Subject Re: change a string where have number after a word
Date 2013-02-06 09:58 -0500
Organization LITTLE WORKS STUDIO
Message-ID <ketr21$nu4$1@dont-email.me> (permalink)
References (2 earlier) <lwbviz8xrn7a$.5zyaw9awc369$.dlg@40tude.net> <kern79$mod$1@dont-email.me> <1gqf3e5pc6vmm.7twr9j9j8d4d.dlg@40tude.net> <kes07f$f9g$1@dont-email.me> <nlmynojaqis9.1h8hz7qasbxov.dlg@40tude.net>

Cross-posted to 3 groups.

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

change a string where have number after a word nawfer <novalidsforspam@alt.al> - 2013-02-05 14:21 +0100
  Re: change a string where have number after a word Salvatore <sal@yojimbo.hack.invalid> - 2013-02-05 14:09 +0000
    Re: change a string where have number after a word nawfer <novalidsforspam@alt.al> - 2013-02-05 18:36 +0100
      Re: change a string where have number after a word Salvatore <sal@yojimbo.hack.invalid> - 2013-02-05 19:30 +0000
      Re: change a string where have number after a word "Jonathan N. Little" <lws4art@gmail.com> - 2013-02-05 14:40 -0500
        Re: change a string where have number after a word nawfer <novalidsforspam@alt.al> - 2013-02-05 20:50 +0100
          Re: change a string where have number after a word "Jonathan N. Little" <lws4art@gmail.com> - 2013-02-05 17:14 -0500
            Re: change a string where have number after a word nawfer <novalidsforspam@alt.al> - 2013-02-06 11:47 +0100
              Re: change a string where have number after a word "Jonathan N. Little" <lws4art@gmail.com> - 2013-02-06 09:58 -0500

csiph-web