Announcement

Collapse
No announcement yet.

Automatically Remove Special Characters in phone number.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Automatically Remove Special Characters in phone number.

    How can we make the Phone number stored with no special character. Through this it can be searched easily.

  • #2
    Actually espo saves phone numbers as entered and also in numeric format only in the database table phone_number.

    If you wish to store a phone number in numeric format only for a custom entity, you can create a beforeSave hook for that entity and include this statement:

    Code:
    $numericPhone = preg_replace('/[^0-9]/', '', $formattedPhoneNumber);

    Comment


    • #3
      Originally posted by telecastg View Post
      Actually espo saves phone numbers as entered and also in numeric format only in the database table phone_number.

      If you wish to store a phone number in numeric format only for a custom entity, you can create a beforeSave hook for that entity and include this statement:

      Code:
      $numericPhone = preg_replace('/[^0-9]/', '', $formattedPhoneNumber);

      It didn't work. This is the code
      Code:
      <?php
      
      namespace Espo\Custom\Entities;
      use Espo\ORM\Entity;
      
      class entest extends \Espo\Core\Templates\Entities\Base
      {
         // protected $entityType = "entest";
          public function beforeSave(Entity $entity, array $options = [])
          {
              $phonenum = preg_replace("/[^0-9]/", "", $phonenum);
          }
      
      }
      
      ?>

      Comment


      • #4
        You need to define the formatted number variable first and then define a NEW variable which will store the "clean" number. In your script you are defining "$phonenum" as a modified string of "$phonenum" which doesn't exist yet.

        See the script: application/Espo/Repositories/PhoneNumber.php

        Code:
                    $number = $entity->get('name');
                    if (is_string($number) && strpos($number, self::ERASED_PREFIX) !== 0) {
                        $numeric = preg_replace('/[^0-9]/', '', $number);
                    } else {
                        $numeric = null;
                    }

        Comment


        • #5
          Originally posted by telecastg View Post
          You need to define the formatted number variable first and then define a NEW variable which will store the "clean" number. In your script you are defining "$phonenum" as a modified string of "$phonenum" which doesn't exist yet.

          See the script: application/Espo/Repositories/PhoneNumber.php

          Code:
          $number = $entity->get('name');
          if (is_string($number) && strpos($number, self::ERASED_PREFIX) !== 0) {
          $numeric = preg_replace('/[^0-9]/', '', $number);
          } else {
          $numeric = null;
          }
          got it working. THANKS for leading me to it.

          Comment


          • telecastg
            telecastg commented
            Editing a comment
            You're welcome :-)
        Working...
        X