How can I grab an integer from the following strings?

co_aut1_last_name,co_aut1_first_name,co_aut1_institution,co_aut1_country
co_aut10_last_name,co_aut10_first_name,co_aut10_institution,co_aut10_country
co_aut100_last_name,co_aut100_first_name,co_aut100_institution,co_aut100_country

so I that only retrieve 1, 10, 100

If I do this:

$string = substr($p,6,1);

I can grab 1 but if the number is 10 or 100 I will only get 1 still

    Well if your string will always look like the above you can grab your information in many different ways. You can explode it with , (comma) as a delimter and parse the first block you get from the explode. Or explore regular expressions they can do the trick for you as well.

    I've included some simple code, this is just an example to get you started working with strings 🙂

    <?php
    
    $str[] = "co_aut1_last_name,co_aut1_first_name,co_aut1_institution,co_aut1_country";
    $str[] = "co_aut10_last_name,co_aut10_first_name,co_aut10_institution,co_aut10_country";
    $str[] = "co_aut100_last_name,co_aut100_first_name,co_aut100_institution,co_aut100_country";
    
    $no = array();
    
    foreach($str as $key => $val)
    {
     $temparr = explode(",", $val);
     $temp = '';
     for($i=0; $i < strlen($temparr[0]); $i++)
     {
    
      if(is_numeric($temparr[0][$i]))
      {
       $temp .= $temparr[0][$i];
      }
     }
     $no[] = (int)$temp;
    }
    
    echo '<pre>';
    print_r($no);
    echo '</pre>';
    ?>
    

    Hope it helps

      Off the top of my head, process the string 1 char at a time, test if it's a number and save it if it is

      $i = 0;
      $j = strlen($string);
      $ns = "";
      $flg = 0;
      // process the string
      do {
         // if the next char is a number include it in the string
         $char = substr($string,$1,1);
         if (is_finite($char)) {
            // check if the last char was a number and insert a comma if it was not
            if ($flg == 0 and (strlen($ns) > 0)) {
              $ns .= ', ';
            }
            // else just add it to the string and flag the number
            $ns .=  $char;
            $flg = 1;
         } 
         // flag it as not a number
         else {
            $flg = 0;
         }
         // increment the offset
         ++$i;
      } while ($i < $j);
      
      

      Should just about do it, but wahtch out for my typos

        Weel, that will teach me to read the question properly before I jump in, I thought it was all one long string; and you can see I'm a VB coder form the way I do this sort of thing.

        My algo will extract ALL the numbers from the string, not just the first instance. I think that Jern has a slicker approach.
        One modification you could make to mine would be to terminate the loop as soon as you've scraped the first number out of the string. Another would be to bung the numbers into an array and test if it was already present before adding any number.

          Humm, I'm all mixed up...

          Ok here is part of my code:

          foreach (array_keys($_POST) as $p) {

          if ($_POST[$p] != '')
          {

          //HOW SHOULD I IMPLEMENT YOUR CODE IN HERE? 		  

          }
          }

          $p can either have co_aut1_last_name or co_aut10_last_name etc....

            // assuming $p holds e.g.:
            // "co_aut1_last_name,co_aut1_first_name,co_aut1_institution,co_aut1_country"
            // "co_aut10_last_name,co_aut10_first_name,co_aut10_institution,co_aut10_country"
            // "co_aut100_last_name,co_aut100_first_name,co_aut100_institution,co_aut100_country"
            foreach (array_keys($_POST) as $p) {
            	if ($_POST[$p] != '') {
            		$temparr = explode(",", $p); //exploding $p by comma delimiter
            		$temp = ''; //this is just a temporary string to hold the number as we build it
            		for($i=0; $i < strlen($temparr[0]); $i++) {//loop through the length of the string
            			if(is_numeric($temparr[0][$i])) {//treat the string as an array of characters and check if a character is a number
            				$temp .= $temparr[0][$i]; //append it to the temporary string
            		}
            	$number_found[] = (int)$temp; //cast the string to an integer and store in your array (this is not a necessary(sp?) step)
            	}
            }
            //and your number_found array should now contain Array(0=>1, 1=>10, 2=>100)
            

              With mine you just insert it into your if

              foreach (array_keys($_POST) as $p) {
              
              if ($_POST[$p] != '')
              {
              
              // my code 
              $string = $_POST[$p];
              
              $i = 0;
              $j = strlen($string);
              $ns = "";
              $flg = 0;
              // process the string
              do {
                 // if the next char is a number include it in the string
                 $char = substr($string,$1,1);
                 if (is_finite($char)) {
                    // check if the last char was a number and insert a comma if it was not
                    if ($flg == 0 and (strlen($ns) > 0)) {
                      $ns .= ', ';
                    }
                    // else just add it to the string and flag the number
                    $ns .=  $char;
                    $flg = 1;
                 } 
                 // flag it as not a number
                 else {
                    $flg = 0;
                 }
                 // increment the offset
                 ++$i;
              } while ($i < $j);
              
              // end my code
              
              // now echo the string to check it
              echo $ns;
              
               }
              }
              
              
              

              By the way, both jernhenrik and I are doing very similar things with this. As I said, my approach is cos my background is VB and VB is very good at processing strings. In vb this would run fast. Dunno bout php.

                jernhenrik,

                I tried your code and got this:

                Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 1 [25] => 1 [26] => 1 [27] => 1 [28] => 1 [29] => 1 [30] => 1 [31] => 1 [32] => 1 [33] => 1 [34] => 1 [35] => 1 [36] => 1 [37] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 1 [44] => 1 [45] => 1 [46] => 1 [47] => 1 [48] => 1 [49] => 0 [50] => 0 [51] => 0 [52] => 0 [53] => 0 [54] => 0 [55] => 1 [56] => 1 [57] => 1 [58] => 1 [59] => 1 [60] => 1 [61] => 1 [62] => 1 [63] => 1 )

                $p is not a string but an individual field name that was on the php page that is called co_aut1_last_name depending on which field names that are not empty.

                $p only contains one field name at a time and not a bunch of them separated by commas...sorry I think I misexplained this...

                  Erm?! Not sure what you are trying to tell us?
                  Does $_POST[$p] hold your string or?

                    Why the overcomplication? There is a tool already built for just this kind of job and it was mentioned in the first reply then duely ignored.

                    Also, I think you both missunderstood the POST part of the question, the first part was badly explained. yoki is talking about $_POST keys not values, and the commas are not part of these keys there are merely delimiting for the example.

                    // the strings
                    $strings;
                    $nums = array();
                    
                    foreach( $strings as $string ) {
                      // Assuming the number you want is the only numeric occurrence in the string
                      if(! preg_match( '/[0-9]+/', $string, $matches ) ) 
                        die("Something went wrong, the string '$string' doesn't have a number in it\n");
                      $nums[] = $matches[0];
                    }
                    

                    Then for the $POST example

                    foreach( $_POST as $key => $value ) {
                      if( preg_match( '/[0-9]+/', $key, $matches ) ) {
                        echo("The number in '$string' is {$matches[0]}\n");
                      }
                    }
                    

                      OK bubblenut, you're probably right. But then we can only answer the question asked: which was "How can I find all the numbers in these stings?"

                      But admit that you worked backwards from the result to the original data, how elee did you figure that the strings were infact keys?

                        OK bubblenut, you're probably right. But then we can only answer the question asked: which was "How can I find all the numbers in these stings?"

                        But admit that you worked backwards from the result to the original data, how elee did you figure that the strings were infact keys?

                        Yet another example where asking a smart quetion would get a smart answer.

                          Thanks bubblenut it worked!!!

                          Roger Ramjet & jernhenrik I sincerely apologize for not explaining this more clearly to you two but I very much appreciate the effort to help me with this!!! 🙂 You guys rule!

                            roger:

                            preg_match_all ( "|[0-9]+|", $text, $matches);

                            foreach ($matches[0] as $number) echo "$number<br />";

                              Now will that capture double digit numbers like 10, 67, etc?

                              Oh yes, and I do know that regular expressions are very powerfull, but I've never worked in an environment where they were needed or used. I've no idea what all the parameters are, and I doubt I'll be ever be bothered to learn since I doubt I'll ever need them.

                                Write a Reply...