Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Thanked 0 Times in 0 Posts

    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 05:36 PM.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts