Strip characters, Including spaces, for tel: link

I’m trying to create a proper ‘tel:’ link for phone numbers that are added in an ACF text field. So I’m just trying to strip extraneous characters, but the spaces are causing a problem.

Existing code:

$html .= ‘<p itemprop=”telephone” class=”member-phone tel”>
<a href=”tel:+1′.preg_replace(“/[^0-9]/”,””,esc_url( $member_phone )).'”>’.esc_attr( $member_phone ).'</a></p>’;

The result of this is:

Phone number added: (323) 555-1212

Resulting tel link: +1323205551212

So I’m guessing the space is interpreted as ‘%20′, then the percentage character is stripped, leaving the extra ’20’.

How do I modify to strip the space properly (and/or the resulting %20)?

Read more here:: Strip characters, Including spaces, for tel: link

Leave a Reply

Your email address will not be published. Required fields are marked *