View Single Post
Old 01-21-2011, 04:34 PM   PM User | #1
Polisurgist
New to the CF scene

 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Polisurgist is an unknown quantity at this point
Duplicate " characters when creating links with preg_replace()

So, the goal here is for a script to turn a ten-digit phone number into a hyperlink, as follows.
Phone number 1234567890
becomes <a href="tel:1-123-456-7890">(123) 456-7890</a>

Testing out just the regex would imply that the following find/replace should work:
Find: (\d{3})(\d{3})(\d{4})
Replace: <a href="tel:1-$1-$2-$3">($1) $2-$3</a>

However, if $val=1234567890, the following function:
$val=preg_replace('/(\d{3})(\d{3})(\d{4})/','<a href="tel:1-$1-$2-$3">($1) $2-$3</a>',$val);
Gives the following result:
<a href=""tel:1-123-456-7890"">(123) 456-7890</a>

Basically, every " in the $replacement string gets doubled.

Any bright ideas on ways around this?

Last edited by Polisurgist; 01-21-2011 at 04:36 PM..
Polisurgist is offline   Reply With Quote