2 questions..

How do I delete all commas from a string?

I tried the explode/implode way, but it slows down the script a lot. I looked for an alternate function -- something like delete() like they have in pascal/delphi, but no luck.

I have a number: 1,813,445 and I just want to remove the commas. Then use that number in an arithmetic equation, and then put the commas back in by using...

number_format($str, 3)

but when I use the above function, I get a number like 1,813,445.000. Is there a way I can receive the number without receiving the .000 and without using substr?

Thank you.

~Ron 🙂

    number_format($str, 3, '.', '')
    

      If you just want to strip commas from a string:

      str_replace(',', '', $str)
      

        Thank you for the first! 🙂

        The str_replace() function will not let be have an empty delimiter. I tried it already. 🙁

        Isn't there some sort of str_delete($str, $from, $to) that I can use?

          RonB wrote:

          The str_replace() function will not let be have an empty delimiter.

          What delimiter? There are no delimiters when using [man]str_replace/man.

          RonB wrote:

          I tried it already.

          I suspect you haven't:

          $string = "Hello, World!";
          echo str_replace(',', '', $string); // Hello World!

            Oh I see. I don't think I typed it in properly.

            but for the number_format() from NogDog, didn't work. Instead, I tried just doing this.

            number_format($number)

            and it automatically put the commas in the right place without showing a decimal point. 🙂

            Thank you very much!

              Write a Reply...