How can I convert Persian/Arabic digits from the string to English digits?












4















I have a website in Persian. The keyboard of my website's users is in Persian (or Arabic) usually. So their password will not get matched sometimes.



I have a function for their username (cell-phone number) which converts Persian/Arabic digits to English:



function convert_digits_to_en($entry){
$fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
return numfmt_parse($fmt, $entry);
}


It will work if the entry contains all digits.



i.e. ۰۹۱۲۳۵۶۵۴۹۸ will be converted to 09123565498 as well.





The problem is when the entry contains both characters and digits (like a password). i.e. test۰۹۱۲. I need to convert it to test0912. My current function returns an empty string for that entry. Any idea how can I fix it?










share|improve this question

























  • I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. Edit: Turns out it was something else, similar but not same: stackoverflow.com/questions/53264154/…

    – Script47
    Nov 15 '18 at 15:35













  • regex is probably best suited for this.

    – noid
    Nov 15 '18 at 15:37






  • 1





    Possible duplicate of convert Persian/Arabic numbers to English numbers

    – Erik Kalkoken
    Nov 15 '18 at 15:44
















4















I have a website in Persian. The keyboard of my website's users is in Persian (or Arabic) usually. So their password will not get matched sometimes.



I have a function for their username (cell-phone number) which converts Persian/Arabic digits to English:



function convert_digits_to_en($entry){
$fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
return numfmt_parse($fmt, $entry);
}


It will work if the entry contains all digits.



i.e. ۰۹۱۲۳۵۶۵۴۹۸ will be converted to 09123565498 as well.





The problem is when the entry contains both characters and digits (like a password). i.e. test۰۹۱۲. I need to convert it to test0912. My current function returns an empty string for that entry. Any idea how can I fix it?










share|improve this question

























  • I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. Edit: Turns out it was something else, similar but not same: stackoverflow.com/questions/53264154/…

    – Script47
    Nov 15 '18 at 15:35













  • regex is probably best suited for this.

    – noid
    Nov 15 '18 at 15:37






  • 1





    Possible duplicate of convert Persian/Arabic numbers to English numbers

    – Erik Kalkoken
    Nov 15 '18 at 15:44














4












4








4








I have a website in Persian. The keyboard of my website's users is in Persian (or Arabic) usually. So their password will not get matched sometimes.



I have a function for their username (cell-phone number) which converts Persian/Arabic digits to English:



function convert_digits_to_en($entry){
$fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
return numfmt_parse($fmt, $entry);
}


It will work if the entry contains all digits.



i.e. ۰۹۱۲۳۵۶۵۴۹۸ will be converted to 09123565498 as well.





The problem is when the entry contains both characters and digits (like a password). i.e. test۰۹۱۲. I need to convert it to test0912. My current function returns an empty string for that entry. Any idea how can I fix it?










share|improve this question
















I have a website in Persian. The keyboard of my website's users is in Persian (or Arabic) usually. So their password will not get matched sometimes.



I have a function for their username (cell-phone number) which converts Persian/Arabic digits to English:



function convert_digits_to_en($entry){
$fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
return numfmt_parse($fmt, $entry);
}


It will work if the entry contains all digits.



i.e. ۰۹۱۲۳۵۶۵۴۹۸ will be converted to 09123565498 as well.





The problem is when the entry contains both characters and digits (like a password). i.e. test۰۹۱۲. I need to convert it to test0912. My current function returns an empty string for that entry. Any idea how can I fix it?







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:41







Martin AJ

















asked Nov 15 '18 at 15:33









Martin AJMartin AJ

2,1521030




2,1521030













  • I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. Edit: Turns out it was something else, similar but not same: stackoverflow.com/questions/53264154/…

    – Script47
    Nov 15 '18 at 15:35













  • regex is probably best suited for this.

    – noid
    Nov 15 '18 at 15:37






  • 1





    Possible duplicate of convert Persian/Arabic numbers to English numbers

    – Erik Kalkoken
    Nov 15 '18 at 15:44



















  • I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. Edit: Turns out it was something else, similar but not same: stackoverflow.com/questions/53264154/…

    – Script47
    Nov 15 '18 at 15:35













  • regex is probably best suited for this.

    – noid
    Nov 15 '18 at 15:37






  • 1





    Possible duplicate of convert Persian/Arabic numbers to English numbers

    – Erik Kalkoken
    Nov 15 '18 at 15:44

















I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. Edit: Turns out it was something else, similar but not same: stackoverflow.com/questions/53264154/…

– Script47
Nov 15 '18 at 15:35







I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. Edit: Turns out it was something else, similar but not same: stackoverflow.com/questions/53264154/…

– Script47
Nov 15 '18 at 15:35















regex is probably best suited for this.

– noid
Nov 15 '18 at 15:37





regex is probably best suited for this.

– noid
Nov 15 '18 at 15:37




1




1





Possible duplicate of convert Persian/Arabic numbers to English numbers

– Erik Kalkoken
Nov 15 '18 at 15:44





Possible duplicate of convert Persian/Arabic numbers to English numbers

– Erik Kalkoken
Nov 15 '18 at 15:44












2 Answers
2






active

oldest

votes


















0














A non-regex solution would be this:



function convert_digits_to_en($input)
{
$arabic = ['۰' => 0, '۱' => 1];

return strtr($input, $arabic);
}

echo convert_digits_to_en('test۱۰۰'); // test100


Note: You'd need to add in the numbers to that array, my Persian / Arabic is not that good.



Live Example



Repl



Reading Material



strtr






share|improve this answer































    0














    you can use your string as array and iterate them like:



    function convert_digits_to_en($entry){
    $result = '';
    $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
    foreach((array)$entry as $value){
    $transform_digit = numfmt_parse($fmt, $value);;
    $result = empty($transform_digit) ? $value : $transform_digit;
    }
    return $result;
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322805%2fhow-can-i-convert-persian-arabic-digits-from-the-string-to-english-digits%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      A non-regex solution would be this:



      function convert_digits_to_en($input)
      {
      $arabic = ['۰' => 0, '۱' => 1];

      return strtr($input, $arabic);
      }

      echo convert_digits_to_en('test۱۰۰'); // test100


      Note: You'd need to add in the numbers to that array, my Persian / Arabic is not that good.



      Live Example



      Repl



      Reading Material



      strtr






      share|improve this answer




























        0














        A non-regex solution would be this:



        function convert_digits_to_en($input)
        {
        $arabic = ['۰' => 0, '۱' => 1];

        return strtr($input, $arabic);
        }

        echo convert_digits_to_en('test۱۰۰'); // test100


        Note: You'd need to add in the numbers to that array, my Persian / Arabic is not that good.



        Live Example



        Repl



        Reading Material



        strtr






        share|improve this answer


























          0












          0








          0







          A non-regex solution would be this:



          function convert_digits_to_en($input)
          {
          $arabic = ['۰' => 0, '۱' => 1];

          return strtr($input, $arabic);
          }

          echo convert_digits_to_en('test۱۰۰'); // test100


          Note: You'd need to add in the numbers to that array, my Persian / Arabic is not that good.



          Live Example



          Repl



          Reading Material



          strtr






          share|improve this answer













          A non-regex solution would be this:



          function convert_digits_to_en($input)
          {
          $arabic = ['۰' => 0, '۱' => 1];

          return strtr($input, $arabic);
          }

          echo convert_digits_to_en('test۱۰۰'); // test100


          Note: You'd need to add in the numbers to that array, my Persian / Arabic is not that good.



          Live Example



          Repl



          Reading Material



          strtr







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 15:44









          Script47Script47

          9,23142246




          9,23142246

























              0














              you can use your string as array and iterate them like:



              function convert_digits_to_en($entry){
              $result = '';
              $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
              foreach((array)$entry as $value){
              $transform_digit = numfmt_parse($fmt, $value);;
              $result = empty($transform_digit) ? $value : $transform_digit;
              }
              return $result;
              }





              share|improve this answer




























                0














                you can use your string as array and iterate them like:



                function convert_digits_to_en($entry){
                $result = '';
                $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
                foreach((array)$entry as $value){
                $transform_digit = numfmt_parse($fmt, $value);;
                $result = empty($transform_digit) ? $value : $transform_digit;
                }
                return $result;
                }





                share|improve this answer


























                  0












                  0








                  0







                  you can use your string as array and iterate them like:



                  function convert_digits_to_en($entry){
                  $result = '';
                  $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
                  foreach((array)$entry as $value){
                  $transform_digit = numfmt_parse($fmt, $value);;
                  $result = empty($transform_digit) ? $value : $transform_digit;
                  }
                  return $result;
                  }





                  share|improve this answer













                  you can use your string as array and iterate them like:



                  function convert_digits_to_en($entry){
                  $result = '';
                  $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
                  foreach((array)$entry as $value){
                  $transform_digit = numfmt_parse($fmt, $value);;
                  $result = empty($transform_digit) ? $value : $transform_digit;
                  }
                  return $result;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 15:48









                  epischepisch

                  304114




                  304114






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322805%2fhow-can-i-convert-persian-arabic-digits-from-the-string-to-english-digits%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python