Phone Number Template Filter

  • I got this from another thread where someone was having an issue with clickable phone numbers. On many occasions Phone numbers are entered on a page in a bunch of different ways. So I wanted to be able to strip the phone number of all non-numerical characters in order to create a clickable phone number that I could use without having to enter the phone number into a template twice. Here is my solution that seems to work.

    First add the following to your config file

    1. define('PERCH_TEMPLATE_FILTERS', true);

    Next open the filters.php file it can be found /perch/addons/templates/filters.php add the following filter and call to the file

    Next I utilize content field to add the phone number visibly to the page and also use it again in the the link tag but strip everything except the numbers

    1. <a href="tel:<perch:content id="phone" filter="phoneclean" />">
    2. <perch:content id="phone" type="text" label="Main Phone Number" help="Do not include letters in your phone number please" />
    3. </a>

    I am not sure how this works internationally but it works well in the United States and can surely be adjusted to fit international requirements. I hope this helps someone out. :burd1fly::burd1fly:

  • No you are correct Drew. I removed letters and added the plus sign in the regular expression of the original answer. That should hopefully fix any international issues.

    I also included info in the Help telling clients to specifically not use letters. So we don't get anything like 1-800-Got-Junk. I don't really want to get into the conversion table because the above solution works for all of my clients. However, if someone else wants to take a stab at it then I would gladly change what I'm using. :thumbup:

  • This converts 1-800-Got-Junk to 18004685865:

  • Drew is such an incredible programer. His approach to solving this issue is just amazing. If you have ever taken the time to look through the core files of Perch or Runway you will find out Drew is just magical at coding and has an understanding which is truly impressive.

    I along with so many other praise you Drew for your gift of coding and that you chose to share that gift with us. :burd1:<3:burd1: