View Full Version : need some regexp help
nlopes
Jun 1st 2004, 9:09 am
Hello,
I need some regexp help, using preg_replace.
Input data:
"%Y-%m-%d" " " "%H:%M:%S"
NULL
"1"
"/tmp"
... and so one...
and it should ouput this:
"%Y-%m-%d %H:%M:%S"
NULL
"1"
"/tmp"
Without using some nasty PHP commands, is there a regexp to replace those chars?, like:
$str = '"%Y-%m-%d" " " "%H:%M:%S"
NULL
"1"
"/tmp"';
echo preg_replace('/^"[^"]*(["]+).*"$/', '', $str);
Note: The above code doesn't work (obviously).
Probably this could be done with some lookahead/lookbehind subexpressions?
Thanks,
Nuno
Help Desk
Jun 1st 2004, 9:36 am
It's not sure if this handles all the possible cases you are looking to handle, but have you tried this?
$str = '"%Y-%m-%d" " " "%H:%M:%S"
NULL
"1"
"/tmp"';
echo preg_replace('/" "/', '', $str);
nlopes
Jun 1st 2004, 10:31 am
Thanks, but it doesn't work... it simplys echoes '': nothing!
I need a regexp to remove the quotes " in the string (but not the quotes in the end and in the begining). and to remove those extra spaces, too.
Any ideas?
Arnica
Jun 1st 2004, 10:43 am
Hi nlopes
To save complexity in the reg expression I would simply use it to replace all ' " ' in the string and then prepend and append ' " ' afterwards. That way you're not having to worry about 'last matches' etc.
Mick :)
nlopes
Jun 1st 2004, 11:56 am
That would broke strings like NULL, that don't have quotes...
digitalpoint
Jun 1st 2004, 12:02 pm
Actually this does works for me:
<?php
$str = '"%Y-%m-%d" " " "%H:%M:%S"
NULL
"1"
"/tmp"';
echo preg_replace('/" "/', '', $str);
?>
Although, probably better to use str_replace(), like so:
<?php
$str = '"%Y-%m-%d" " " "%H:%M:%S"
NULL
"1"
"/tmp"';
echo str_replace('" "', '', $str);
?>
nlopes
Jun 1st 2004, 12:04 pm
Thanks Shawn. Your str_replace() might work. I'll test it tomorrow in my program.
Bah,... But I really wanted some complicated regexp :) Something less specific to this case.... But lets see if your solution works!! :D
Arnica
Jun 1st 2004, 5:01 pm
Bah,... But I really wanted some complicated regexp :) Something less specific to this case.... But lets see if your solution works!! :D
How's this for complicated?
<?php
$str = '"%Y-%m-%d" " " "%H:%M:%S"
NULL
"1"
"/tmp"';
echo preg_replace('/(\")?([^\"]+)(?:\"| )*([^\"]* (?:\"?[\n\r\f]?))/g', '$1$2 ', $str);
?>
Don't do PHP (Die hard Javascript ASP programmer :eek: ) but this should do the job and handle any variations to the input. Let me know if it does.
NB There is some overkill here in that the ' " ' don't need to be escaped and you could probably get away with fewer parameters to detect the line break!
Mick
nlopes
Jun 2nd 2004, 11:17 am
Thank you for your help!
I've made a general preg_match, which is what I wanted:
if(preg_match_all('/"([^"]+)"/S', $default, $match) > 1) {
$st ='';
foreach($match[1] as $add) {
$st .= $add;
}
$default = '"' . $st . '"';
}
Full program at: http://testes.aborla.net/ini-update.php
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.