regex with preg_replace()function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zzclare
Forum Newbie
Posts: 11
Joined: Wed Nov 10, 2010 5:22 am

regex with preg_replace()function

Post 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
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: regex with preg_replace()function

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with preg_replace()function

Post by AbraCadaver »

If your strings are consistently decimal numbers, then something like this might work:

Code: Select all

$string2 = preg_replace('/([^ ]+[ ][^ ]+)[ ]/', '$1,', $string1);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
zzclare
Forum Newbie
Posts: 11
Joined: Wed Nov 10, 2010 5:22 am

Re: regex with preg_replace()function

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with preg_replace()function

Post 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]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply