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
easy regex problem
Moderator: General Moderators
Re: easy regex problem
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:
-Greg
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);
}Re: easy regex problem
Thanks Greg - That worked!
-KC
-KC
Re: easy regex problem
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
even better! Never realized it could accept an array.