How do I filter this in array?
I do have tags like this
search100
web250
seo36
analytics5060
traffic8000
web2.0
I want remove numbers from this tag so I can use this code in php
preg_replace("/\d+$/gm", "", input)
but I want to keep web2.0
without intact...how do I filter this when I am using a loop..I d开发者_运维问答o have more than 100k tags like this.
You could use the pattern /(\w)\d+$/m
and $1
as replacement:
preg_replace('/(\w)\d+$/m', '$1', $input)
This pattern requires that there is at least one word character before the sequence of digits.
And to apply this replacement on each element of an array use array_map
:
array_map(function($elem) { return preg_replace('/(\w)\d+$/m', '$1', $elem); }, $arr);
If you can’t use an anonymous function (available since PHP 5.3) like in my example, you can either define a separate function, use create_function
instead or just use a foreach
.
From your vague description and the small sample, it seems you could just use:
$input = preg_replace("/\d\d+$/m", "", $input);
This would spare the 2.0 suffix, because it looks for two consecutive numbers as minimum. But another way to accomplish it would be a negative lookbehind (?<!2\.)\d+$
精彩评论