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
Switch to threaded view of this topic Create a new topic Submit Reply
change a string where have number after a word [message #180341] Tue, 05 February 2013 13:21 Go to next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
Member
"arts 4, tested 4, passenger 2"

"arts 4, tested 4, passenger 2, arrived 2"

"arrived 2, tested 4, passenger 2, arts 4"

how can change this type of string for to have only the word with 4 at the
end?


so
"arts 4, tested 4"
first steps taht remove the words with 2 at the end;
and 2nd steps for to have
"arts, tested"

and if with regex the solution is also more fast
Re: change a string where have number after a word [message #180345 is a reply to message #180341] Tue, 05 February 2013 14:09 Go to previous messageGo to next message
Salvatore is currently offline  Salvatore
Messages: 38
Registered: September 2012
Karma: 0
Member
On 2013-02-05, nawfer <novalidsforspam(at)alt(dot)al> wrote:
> "arts 4, tested 4, passenger 2"
>
> "arts 4, tested 4, passenger 2, arrived 2"
>
> "arrived 2, tested 4, passenger 2, arts 4"
>
> how can change this type of string for to have only the word with 4 at the
> end?
>
>
> so
> "arts 4, tested 4"
> first steps taht remove the words with 2 at the end;
> and 2nd steps for to have
> "arts, tested"
>
> and if with regex the solution is also more fast

To begin, it's far easier to use a regular expression. The regex I will
use is the following:

/(\w+)\s(4,|4$)/

And here's the PHP code I wrote up:

<?php
$string = 'arrived 2, tested 4, passenger 2, arts 4';
$regex = '/(\w+)\s(4,|4$)/';
$matches = array();

if (preg_match_all($regex, $string, $matches)) {
$actualMatches = array_shift($matches);
$string = implode(' ', $actualMatches);
}

echo "$string\n";
?>

I'm sure that there's a better way to do this, but my method appears to
work well.

--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
Re: change a string where have number after a word [message #180348 is a reply to message #180345] Tue, 05 February 2013 17:36 Go to previous messageGo to next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
Member
for to complete the code
if I have
"arts color 4, tested well 4, passenger 2, arrived 2"

or
"arts color bb 4, tested well 4, passenger 2, arrived 2"


extract this
"arts color bb 4, tested well 4"

at moment extract so
bb 4, well 4,


and also how have this
"arts color bb, tested well"
Re: change a string where have number after a word [message #180355 is a reply to message #180348] Tue, 05 February 2013 19:30 Go to previous messageGo to next message
Salvatore is currently offline  Salvatore
Messages: 38
Registered: September 2012
Karma: 0
Member
On 2013-02-05, nawfer <novalidsforspam(at)alt(dot)al> wrote:
> for to complete the code
> if I have
> "arts color 4, tested well 4, passenger 2, arrived 2"
>
> or
> "arts color bb 4, tested well 4, passenger 2, arrived 2"
>
>
> extract this
> "arts color bb 4, tested well 4"
>
> at moment extract so
> bb 4, well 4,
>
>
> and also how have this
> "arts color bb, tested well"

In that case, I would do the following:

1. Explode the string using "," as the delimiter.
2. Remove any array elements in the split string that do not match /\s4$/.
3. Implode the string.

--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
Re: change a string where have number after a word [message #180356 is a reply to message #180348] Tue, 05 February 2013 19:40 Go to previous messageGo to next message
Jonathan N. Little is currently offline  Jonathan N. Little
Messages: 12
Registered: February 2011
Karma: 0
Junior Member
nawfer wrote:
> for to complete the code
> if I have
> "arts color 4, tested well 4, passenger 2, arrived 2"
>
> or
> "arts color bb 4, tested well 4, passenger 2, arrived 2"
>
>
> extract this
> "arts color bb 4, tested well 4"
>
> at moment extract so
> bb 4, well 4,
>
>
> and also how have this
> "arts color bb, tested well"
>


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?

2) Do the you want and NON-4 numeric value !=2 and make it 4?

3) Will the numeric values only be a single digit like 3 and !>9 like 15?

4) Do you need the result to be a single string or are you using this in
an array?

If I assume you have a CVA string and you want to change any single
digit trialling number !=4 to be 4 and you need to have it back as
single CSV string then one way.

Depends on your conditions

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';
}

function optional_multidigit($v){
// Change if trailing one or more digit number
return preg_replace('/\d+$/', '4', $v);
}

function only_if_2($v){
// Change only if trailing digit is 2
return preg_replace('/2$/', '4', $v);
}

$before="arts color bb 4, tested well 4, passenger 2, arrived 2, not_two
5, multidigit 12";

$a=explode(',', $before);

print "String before: $before\n";

print 'Only single digit: ' . implode(',',
array_map("always_single_digit", $a)) . "\n";

print 'Possibly multidigit: ' . implode(',',
array_map("optional_multidigit", $a)). "\n";

print 'Only if #2: ' . implode(',', array_map("only_if_2", $a)). "\n";


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Re: change a string where have number after a word [message #180357 is a reply to message #180356] Tue, 05 February 2013 19:50 Go to previous messageGo to next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
Member
> 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
Re: change a string where have number after a word [message #180363 is a reply to message #180357] Tue, 05 February 2013 22:14 Go to previous messageGo to next message
Jonathan N. Little is currently offline  Jonathan N. Little
Messages: 12
Registered: February 2011
Karma: 0
Junior Member
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));


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
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 next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
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
)
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: 0
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
  Switch to threaded view of this topic Create a new topic Submit Reply
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 05:53:41 GMT 2024

Total time taken to generate the page: 0.06207 seconds