Comma separated numerical values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • For example John Smith

    Comma separated numerical values

    My source file contains numerical information shaped like this:

    32,567,90
    32,689,78
    et cetera


    If I use (longint)$numst r I get 32 as value (logically).
    How can I convince php to ignore the , in these strings ?
    Probably in the manual, but I could not find an entry for this. Don't know
    how to call this problem.
    TIA
    Pjotr


  • jerrygarciuh

    #2
    Re: Comma separated numerical values

    Pjotr,

    One way would be to strip them out like so:

    $newNum = preg_replace('/,/', '', $origNum);

    Then do the rest using $newNum.

    HTH

    jg


    "For example John Smith" <zpunkfezt@xz4a ll.nl> wrote in message
    news:40fbbe0e$0 $23867$14726298 @news.sunsite.d k...[color=blue]
    > My source file contains numerical information shaped like this:
    >
    > 32,567,90
    > 32,689,78
    > et cetera
    >
    >
    > If I use (longint)$numst r I get 32 as value (logically).
    > How can I convince php to ignore the , in these strings ?
    > Probably in the manual, but I could not find an entry for this. Don't know
    > how to call this problem.
    > TIA
    > Pjotr
    >
    >[/color]


    Comment

    • Westcoast Sheri

      #3
      Re: Comma separated numerical values

      jerrygarciuh wrote:
      [color=blue]
      > Pjotr,
      >
      > One way would be to strip them out like so:
      >
      > $newNum = preg_replace('/,/', '', $origNum);
      >
      > Then do the rest using $newNum.
      >
      > HTH[/color]

      Is not "str_replac e" faster?
      $newNum = str_replace(',' ,'',$origNum);


      Comment

      • Pjotr Wedersteers

        #4
        Re: Comma separated numerical values

        Westcoast Sheri wrote:[color=blue]
        > jerrygarciuh wrote:
        >[color=green]
        >> Pjotr,
        >>
        >> One way would be to strip them out like so:
        >>
        >> $newNum = preg_replace('/,/', '', $origNum);
        >>
        >> Then do the rest using $newNum.
        >>
        >> HTH[/color]
        >
        > Is not "str_replac e" faster?
        > $newNum = str_replace(',' ,'',$origNum);[/color]

        Thanks guys, both options work, and to satisfy your curiosity:
        Using str_replace and preg_replace -at least in my example- took the same
        amount of time.
        I ran each about ten times, and all execution times are in the same range.
        In this example the replace is called about 27k times, a reasonably
        significant amount to judge execution speed I'd say.
        I used a stopwatch based on microtime (), the script runs on a P3-500Mhz,
        LAMP.
        I'll be using str_replace from here, as I am much less comfortable with
        regexp (yet).

        Actually I would have guessed PHP has a function specifically for
        number-(un)formatting, but hey, this works fine!
        Thanks again!
        Pjotr


        Comment

        Working...