Automatically Remove Special Characters in phone number.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ciryaj
    Member
    • Mar 2018
    • 45

    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.
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #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

    • ciryaj
      Member
      • Mar 2018
      • 45

      #3
      Originally posted by telecastg
      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

      • telecastg
        Active Community Member
        • Jun 2018
        • 907

        #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

        • ciryaj
          Member
          • Mar 2018
          • 45

          #5
          Originally posted by telecastg
          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...