Skip to content

Instantly share code, notes, and snippets.

@FreshLondon
Created April 11, 2019 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FreshLondon/34a3edb614280b697c5ab8958c4e0ffb to your computer and use it in GitHub Desktop.
Save FreshLondon/34a3edb614280b697c5ab8958c4e0ffb to your computer and use it in GitHub Desktop.
Format ACF phone number in a text field
<?
/*
In this example our ACF field is 'footer_phone_number'
*
$original = '+44 (0)1234 567 890';
$original = '0044 01234 567 890';
$original = '01234 567 890';
$original = '44 1234 567 890';
Result should always be:
'+441234567890
*/
$original_phone = get_field('footer_phone_number', 'options-footer');
// Strip out all characters apart from numbers
$phone = preg_replace('/[^0-9]+/', '', $original_phone);
// Remove the 2 digit international code (+44)
if (substr($phone, 0, 2) == '44') {
$phone = substr($phone, 2);
}
// Remove the 4 digit international code (0044)
if (substr($phone, 0, 4) == '0044') {
$phone = substr($phone, 4);
}
// Remove the initial Zero from the number
// Some people write it in international numbers like this: +44 (0)1234 567 890
// But it shouldn't be entered when dialling
if (substr($phone, 0, 1) == '0') {
$phone = substr($phone, 1);
}
// Add the international prefix
$phone = '+44'.$phone;
?>
<div class="container">
<div class="row">
<div class="col footer-contact-links">
<a class="footer-links-number" href="tel:<?= $phone; ?>"><?= $original_phone; ?></a>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment