Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #8275 > unrolled thread
| Started by | nawfer <novalidsforspam@alt.al> |
|---|---|
| First post | 2013-02-05 14:21 +0100 |
| Last post | 2013-02-06 09:58 -0500 |
| Articles | 9 — 3 participants |
Back to article view | Back to comp.lang.php
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
| From | nawfer <novalidsforspam@alt.al> |
|---|---|
| Date | 2013-02-05 14:21 +0100 |
| Subject | change a string where have number after a word |
| Message-ID | <a4mqpnjpnuxm.100ko7fi3dxss.dlg@40tude.net> |
"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
[toc] | [next] | [standalone]
| From | Salvatore <sal@yojimbo.hack.invalid> |
|---|---|
| Date | 2013-02-05 14:09 +0000 |
| Message-ID | <ker3qg$1jpu$1@yojimbo.hack> |
| In reply to | #8275 |
On 2013-02-05, nawfer <novalidsforspam@alt.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++
[toc] | [prev] | [next] | [standalone]
| From | nawfer <novalidsforspam@alt.al> |
|---|---|
| Date | 2013-02-05 18:36 +0100 |
| Message-ID | <lwbviz8xrn7a$.5zyaw9awc369$.dlg@40tude.net> |
| In reply to | #8279 |
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"
[toc] | [prev] | [next] | [standalone]
| From | Salvatore <sal@yojimbo.hack.invalid> |
|---|---|
| Date | 2013-02-05 19:30 +0000 |
| Message-ID | <kermkb$1rhr$1@yojimbo.hack> |
| In reply to | #8282 |
On 2013-02-05, nawfer <novalidsforspam@alt.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++
[toc] | [prev] | [next] | [standalone]
| From | "Jonathan N. Little" <lws4art@gmail.com> |
|---|---|
| Date | 2013-02-05 14:40 -0500 |
| Message-ID | <kern79$mod$1@dont-email.me> |
| In reply to | #8282 |
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
[toc] | [prev] | [next] | [standalone]
| From | nawfer <novalidsforspam@alt.al> |
|---|---|
| Date | 2013-02-05 20:50 +0100 |
| Message-ID | <1gqf3e5pc6vmm.7twr9j9j8d4d.dlg@40tude.net> |
| In reply to | #8290 |
> 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
[toc] | [prev] | [next] | [standalone]
| From | "Jonathan N. Little" <lws4art@gmail.com> |
|---|---|
| Date | 2013-02-05 17:14 -0500 |
| Message-ID | <kes07f$f9g$1@dont-email.me> |
| In reply to | #8291 |
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
[toc] | [prev] | [next] | [standalone]
| From | nawfer <novalidsforspam@alt.al> |
|---|---|
| Date | 2013-02-06 11:47 +0100 |
| Message-ID | <nlmynojaqis9.1h8hz7qasbxov.dlg@40tude.net> |
| In reply to | #8297 |
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
)
[toc] | [prev] | [next] | [standalone]
| From | "Jonathan N. Little" <lws4art@gmail.com> |
|---|---|
| Date | 2013-02-06 09:58 -0500 |
| Message-ID | <ketr21$nu4$1@dont-email.me> |
| In reply to | #8307 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web