python72 Posted November 29, 2010 Share Posted November 29, 2010 I need to find first digit (any number 0 - 9) in a string. I thing I should be able to use: $NumberPosition = strpos($Source, "/[0-9]/", $NumberPosition+1); but it does not work, wonder what I am doing wrong? Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/ Share on other sites More sharing options...
kenrbnsn Posted November 29, 2010 Share Posted November 29, 2010 You're using the wrong function. strpos does not take a regular expression as an argument. You probably want to look at preg_match or preg_match_all. Ken Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140748 Share on other sites More sharing options...
python72 Posted November 29, 2010 Author Share Posted November 29, 2010 I need to know position of the digit withing string. Would preg_match return the position of the digit? Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140752 Share on other sites More sharing options...
BlueSkyIS Posted November 29, 2010 Share Posted November 29, 2010 <?php $str = "hello5world"; $parts = str_split($str); $first_num = -1; $num_loc = 0; foreach ($parts AS $a_char) { if (is_numeric($a_char)) { $first_num = $num_loc; break; } $num_loc++; } if ($first_num > -1) { echo "found number in $str at index $num_loc <br />"; } else { echo "no numbers found in $str"; } ?> Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140756 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 If all you need to do is return the position of the first digit in the string, this works. Zero is the position of the first character, but that's easy enough to change. Don't really know if this would more or less efficient than a pattern match, though. <?php $string = 'This is a string with a 9 number 8.'; $count = strlen($string); $i = 0; while( $i < $count ) { if( ctype_digit($string[$i]) ) { echo "First digit found at position $i."; return; } $i++; } ?> Returns: First digit found at position 24. Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140757 Share on other sites More sharing options...
BlueSkyIS Posted November 29, 2010 Share Posted November 29, 2010 Pikachu2000's is more concise as usual. :-) Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140758 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 Yours takes into consideration a string that doesn't contain a number at all, though. Mine fails that test. Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140762 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2010 Share Posted November 29, 2010 If you had read the manual page for preg_match you would know the answer to your question. No it does not, but it does return the matched string, so you can create a function that combines preg_match & strpos. Something like this: <?php function digpos($str) { $pat = '(\d+)'; preg_match($pat,$str,$matches); return(array($matches[0],strpos($str,$matches[0]))); } $tests = array('sdfj234skdfl;s','this 1s a test','xxxx'); foreach ($tests as $str) { list($matstr,$matpos) = digpos($str); if ($matpos !== false) { echo "The digits [$matstr] were found at position [$matpos] in the string [$str]<br>\n"; } else { echo "No digits were found in the string [$str]<br>\n"; } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/220097-how-to-find-first-number-0-9-in-a-string/#findComment-1140763 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.