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 #180356 is a reply to message #180348] Tue, 05 February 2013 19:40 Go to previous messageGo to previous message
Jonathan N. Little is currently offline  Jonathan N. Little
Messages: 12
Registered: February 2011
Karma:
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
[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 10:34:01 GMT 2024

Total time taken to generate the page: 0.03141 seconds