vinpkl Posted July 13, 2009 Share Posted July 13, 2009 hi all i have a string like "123 abc 45". i am using the below code to extract numeric from string. The result i get for numeric value is "12345". How can i get the result as "123" and "45" as separate two keywords. <?php $string = "123 abc 45"; $chars = ''; $nums = ''; for ($index=0;$index<strlen($string);$index++) { if(isNumber($string[$index])) $nums .= $string[$index]; else $chars .= $string[$index]; } echo "Chars: $chars<br>Nums: $nums"; function isNumber($c) { return preg_match('/[0-9]/', $c); } ?> vineet Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/ Share on other sites More sharing options...
Andy-H Posted July 13, 2009 Share Posted July 13, 2009 http://php.net/ctype_digit Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-874338 Share on other sites More sharing options...
vinpkl Posted July 13, 2009 Author Share Posted July 13, 2009 http://php.net/ctype_digit hi Andy i saw the link you gave but they have no where explained how to separate the two numeric values that we get. what they are checking is about numeric and alpha numeric values. that i have already done. vineet Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-874341 Share on other sites More sharing options...
Andy-H Posted July 13, 2009 Share Posted July 13, 2009 <?php $string = "123 abc 45"; $chars = ''; $nums = ''; for ($index=0; $index<strlen($string); $index++) { if( ctype_digit($string[$index]) ) { if ($str) $nums .= ' '; $nums .= $string[$index]; $str = False; }else{ if (!$str) $chars.= ' '; $chars .= $string[$index]; $str = True; } } echo "Chars: $chars<br >Nums: $nums"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-874357 Share on other sites More sharing options...
vinpkl Posted July 13, 2009 Author Share Posted July 13, 2009 hi andy thanks. it works great. vineet Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-874371 Share on other sites More sharing options...
Andy-H Posted July 13, 2009 Share Posted July 13, 2009 no problem Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-874373 Share on other sites More sharing options...
Falakistan Posted December 17, 2015 Share Posted December 17, 2015 <?php $string="123 abc 45"; preg_match_all("%[0-9]{1,}%",$string,$matches); foreach($matches[0] as $number) { echo $number."<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-1528098 Share on other sites More sharing options...
QuickOldCar Posted December 17, 2015 Share Posted December 17, 2015 No reason to drag up 6 year old posts Quote Link to comment https://forums.phpfreaks.com/topic/165753-solved-extract-numeric-values-from-string/#findComment-1528099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.