Page 1 of 1

regex with preg_replace()function

Posted: Tue Mar 08, 2011 8:23 am
by zzclare
Hello,

I need to modify a string. The original string is like
$string1="34.5 333.5 34.3 456.4 667.5 456.8 ";
I want to add comma(,) to every second white space so the string will look like
$string2="34.5 333.5,34.3 456.4,667.5 456.8" ;
So far i think preg_replace()function could be used. So the pattern look like this
pattern1= "/^[^0-9]+[.][0-9]+[ ][0-9]+[.][0-9]+[ ]" will be replace by
pattern2="/^[^0-9]+[.][0-9]+[ ][0-9]+[.][0-9]+[ ][,]"

in pattern1 =number+dot+number+whitespace+number+dot+number+whitespace
in pattern2 = number+dot+number+whitespace+number+dot+number+comma

but it doesn´t work.I think my regex is still wrong. Does anyone familiar with this problem.
Thanks

Re: regex with preg_replace()function

Posted: Tue Mar 08, 2011 9:14 am
by Kadanis
You need to escape your dots for starters. Dots have meaning in regular expressions so add a \ before them if you want a literal dot

Eg. /^[^0-9]+[\.][0-9]+[ ][0-9]+[\.][0-9]+[ ]/

I haven't actually tested your regex though, so don't know if it will work, but that is the first thing i noticed.

Re: regex with preg_replace()function

Posted: Tue Mar 08, 2011 11:06 am
by AbraCadaver
If your strings are consistently decimal numbers, then something like this might work:

Code: Select all

$string2 = preg_replace('/([^ ]+[ ][^ ]+)[ ]/', '$1,', $string1);

Re: regex with preg_replace()function

Posted: Wed Mar 09, 2011 3:18 am
by zzclare
Thanks. it works. But could you explain me what´s the meaning of $1.
here is web page I searched for regex http://www.webcheatsheet.com/php/regula ... ssions.php
and it saids dollar sign ($) is used to match strings that end with the given pattern. What´s 1 means?
Now i have to update your code, and change the string into the format
(34.5,333.5),(34.3,456.4),(667.5 ,456.8)
If you could explain what´s $1 means, I think I can find the answer by myself.
Thanks alot

Re: regex with preg_replace()function

Posted: Wed Mar 09, 2011 11:51 am
by AbraCadaver
zzclare wrote:Thanks. it works. But could you explain me what´s the meaning of $1.
here is web page I searched for regex http://www.webcheatsheet.com/php/regula ... ssions.php
and it saids dollar sign ($) is used to match strings that end with the given pattern. What´s 1 means?
Now i have to update your code, and change the string into the format
(34.5,333.5),(34.3,456.4),(667.5 ,456.8)
If you could explain what´s $1 means, I think I can find the answer by myself.
Thanks alot
That's a backreference and that is not a pattern, it is the replacement string. So $1 means to replace with what was captured in the first (capture group). If we had more capture groups then $2 would refer to the second capture group, etc. \\1 or \\2 can also be used.

Code: Select all

$string = 'do now';
$pattern = '/([\w]+) ([\w]+)/';
$replacement = '$1 something $2';
$new_string = preg_replace($pattern, $replacement, $string);
[text]do something now[/text]