简体   繁体   中英

preg_replace find phone number and remove spaces

I have this function to find phone numbers and replace them with a clickable link, I want to remove spaces from the phone number because the numbers order breaks when it is mixed with RTL (right to left) text:

$content_data = preg_replace('!(\b\+?[0-9()\[\]./ -]{7,17}\b|\b\+?[0-9()\[\]./ -]{7,17}\s+(extension|x|#|-|code|ext)\s+[0-9]{1,6})!i', '<a class="external text-ltr" href="tel:$1">$1</a>', $content_data);

How should I modify the code to remove spaces ' ' from the phone numbers?

Thanks.

How about:

EDITED:

$content_data = preg_replace_callback(
  '!(\b\+?[0-9()\[\]./ -]{7,17}\b|\b\+?[0-9()\[\]./ -]{7,17}\s+(extension|x|#|-|code|ext)\s+[0-9]{1,6})!i',
  function ($matches){
    $tel=str_replace(" ", "", $matches[0]);
    return '<a class="external text-ltr" href="tel:'.$tel.'">'.$tel.'</a>';
  },
  $content_data
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM