Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. extract numeric values from string

extract numeric values from string

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shaina2231
    wrote on last edited by
    #1

    How to extract Numeric values from String plz help

    C F M 3 Replies Last reply
    0
    • S shaina2231

      How to extract Numeric values from String plz help

      C Offline
      C Offline
      cjoki
      wrote on last edited by
      #2

      not sure if you mean you have a string with a number in it and need to pull just the string or if you have a number returned as a string. but for the first you can separate the string using explode and then looping through the returned array and checking for is_numeric returning true and extracting that. the second should be able to be used directly $a = '1'; $echo 1 + $a; but if there is a issue with it, you can use an explicit cast like in C (int)$a for example.

      1 Reply Last reply
      0
      • S shaina2231

        How to extract Numeric values from String plz help

        F Offline
        F Offline
        fly904
        wrote on last edited by
        #3

        In the PHP documentation of Strings[^] there is a little snippet just over a third of the way down called "String conversion to numbers", which may help you. Other than that, you could always go through the string char by char ;P

        If at first you don't succeed, you're not Chuck Norris.

        1 Reply Last reply
        0
        • S shaina2231

          How to extract Numeric values from String plz help

          M Offline
          M Offline
          Marc Firth
          wrote on last edited by
          #4

          I know this is a few days old but I thought I'd mention some new functionality in PHP. PHP Filters. Specifically:

          <?php
          $value01 = '123abc456def';
          echo filter_var($value01, FILTER_SANITIZE_NUMBER_INT);
          ?>

          Will Output:

          123456

          Portfolio | Web Design, Web Hosting & IT Support

          F 1 Reply Last reply
          0
          • M Marc Firth

            I know this is a few days old but I thought I'd mention some new functionality in PHP. PHP Filters. Specifically:

            <?php
            $value01 = '123abc456def';
            echo filter_var($value01, FILTER_SANITIZE_NUMBER_INT);
            ?>

            Will Output:

            123456

            Portfolio | Web Design, Web Hosting & IT Support

            F Offline
            F Offline
            fly904
            wrote on last edited by
            #5

            That's very useful. Bookmarked this for future reference, just in case.

            If at first you don't succeed, you're not Chuck Norris.

            M 1 Reply Last reply
            0
            • F fly904

              That's very useful. Bookmarked this for future reference, just in case.

              If at first you don't succeed, you're not Chuck Norris.

              M Offline
              M Offline
              Marc Firth
              wrote on last edited by
              #6

              Yeah. Good tut here[^].

              Portfolio | Web Design, Web Hosting & IT Support

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups