Search code examples
phpdrupaldrupal-7drupal-modules

Display multiple phone numbers with dashes


I am struck into a problem in which situation is that I have a customer role in my website, for whom I have to save their phone numbers and alternative phone numbers but because customers belong to US, due to which there we need to poy dashes in their phone numbers. for example: 111-222-3333. I also have integrated a referral system in the site in which I am able to refer friends using their phone numbers.I can send referral to multiple phone numbers at once. For example: 111-222-3333, 111-333-444, 444-233-9330 But the condition is that the phone number which we are referring should not be in customer's profile (The user should not be already registered with that number). The problem arises when the customer's phone number is saved with dashes but because customer can refer multiple comma separated phone numbers, due to which I am unable to provide a pattern of dashes to fill phone number in. But now customer can send referral to phone number which is not dashed like (1112223333) which if anyone is already registered with the same phone number and having dashes like (111-222-3333). In such case it wont be matched with already registered phone number. And the Problem is about displaying the multiple phone numbers in dashes while referring. Can anyone please give solution for this. How to display multiple phone numbers with dashes. Sorry if you have any trouble in understanding the concept but I'll explain it again. Any help would be appreciated. Sorry I've edited the question. I didn't metion the actual problem


Solution

  • It seems like you could fix the issue by stripping the phone numbers of all non-numeric characters before comparing them.

    You can strip all non-numeric characters from a string by using preg_replace as follows:

    $str = preg_replace('/[^0-9]+/', '', $str);
    

    Here's a short example of a comparaison:

    <?php
    
    $phoneNumber1 = "(418) 515-4184";
    $phoneNumber2 = "4185154184";
    if (preg_replace('/[^0-9]+/', '', $phoneNumber1) == preg_replace('/[^0-9]+/', '', $phoneNumber1))
    {
        echo "Phone numbers are the same";
    }
    else
    {
        // do stuff
    }
    
    ?>
    

    Hope that helps!