Page 1 of 1

easy regex problem

Posted: Mon Nov 28, 2011 3:15 pm
by kc11
Hi,

I'm new to regex. I have an array with the elements like so:

Here is my $keys array :


array
'IP:Port' => string '216.114.46.6:3128' (length=17)
'Response Time' => string '0' (length=1)
'Server Speed' => string '0' (length=1)


I would like to replace the ':' with somthing else (like '_' )

here is my regex:

preg_replace('/":"/', "_", $keys);

Its not working. Can anyone show me what I'm doing wrong?

Thanks in advance,

KC

Re: easy regex problem

Posted: Mon Nov 28, 2011 4:05 pm
by twinedev
1. You are passing it an array ($keys) instead of a string to process.
2. You telling it to replace quote - colon - quote with an underscore

since you are replacing specific chractersets, you can just use str_replace(), trying something like this:

Code: Select all

foreach($keys as $tKey=>$tValue) {
    $keys[$tKey] = str_replace(':','_',$tValue);
}
-Greg

Re: easy regex problem

Posted: Tue Nov 29, 2011 8:55 pm
by kc11
Thanks Greg - That worked!

-KC

Re: easy regex problem

Posted: Tue Nov 29, 2011 10:11 pm
by McInfo
The third argument for str_replace() can be an array; so this has the same result.

Code: Select all

$keys = str_replace(':', '_', $keys);

Re: easy regex problem

Posted: Wed Nov 30, 2011 1:58 pm
by twinedev
even better! Never realized it could accept an array.