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 #180345 is a reply to message #180341] Tue, 05 February 2013 14:09 Go to previous messageGo to previous message
Salvatore is currently offline  Salvatore
Messages: 38
Registered: September 2012
Karma:
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++
[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: Fri Sep 20 07:39:50 GMT 2024

Total time taken to generate the page: 0.05802 seconds