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


Groups > comp.lang.php > #8290

Re: change a string where have number after a word

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
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 Tue, 05 Feb 2013 14:40:55 -0500
Organization LITTLE WORKS STUDIO
Lines 78
Message-ID <kern79$mod$1@dont-email.me> (permalink)
References <a4mqpnjpnuxm.100ko7fi3dxss.dlg@40tude.net> <ker3qg$1jpu$1@yojimbo.hack> <lwbviz8xrn7a$.5zyaw9awc369$.dlg@40tude.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Tue, 5 Feb 2013 19:40:25 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="80d5b40c84ec70a6ddacffcefe893b70"; logging-data="23309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZBRlYD9CUIIshyHW5q0oCa2Y0aLKai8Q="
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0 SeaMonkey/2.15.2
In-Reply-To <lwbviz8xrn7a$.5zyaw9awc369$.dlg@40tude.net>
X-Dan Yes Dan this is a Winbox
X-Face o[H8T0h*NGH`K`P)s+4PmYlcy|GNl`~+L6Fi.m:%15m[c%{C7V-ump|WiCYPkQ+hFJhq;XW5^1Rg_El'"fE$~AcYW$Pq\yeh9K_-dJqlQ5\y2\;[yw5DYCtOtsf_.TUy}0U\oL^>[3Y#{AP2^o'bG`bwj`]]UNpCxY\(~xK9b+uZKxrb*4-rkD+
Cancel-Lock sha1:v8hD1VJ0TytoZ5RmJsaciTi1mTQ=
Xref csiph.com comp.lang.php:8290

Cross-posted to 3 groups.

Show key headers only | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next 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