npsari Posted January 23, 2010 Share Posted January 23, 2010 Hello, This script is useful. It extracts the numbers. I am trying to echo the first number only! <?php function extract_numbers($string) { preg_match_all('/([\d]+)/', $string, $match); return $match[0]; } $string = '32hello42good32morning'; $numbers_array = extract_numbers($string); echo '<pre>'; print_r($numbers_array); echo "</pre>"; $get_first_number = $array["$numbers_array"][0]; echo "$get_first_number"; ?> $get_first_number is not showing $get_first_number should be 32 Can you please fix the script for me? Link to comment https://forums.phpfreaks.com/topic/189557-array-little-problem/ Share on other sites More sharing options...
PHP Monkeh Posted January 23, 2010 Share Posted January 23, 2010 $get_first_number = $numbers_array[0]; echo $get_first_number; You haven't created a multi-dimensional array, nor have you created an array called $array - so use the above code rather than your last two lines. Link to comment https://forums.phpfreaks.com/topic/189557-array-little-problem/#findComment-1000540 Share on other sites More sharing options...
Adam Posted January 23, 2010 Share Posted January 23, 2010 Why not just use: preg_replace('/[^0-9]/', '', $string); Link to comment https://forums.phpfreaks.com/topic/189557-array-little-problem/#findComment-1000544 Share on other sites More sharing options...
npsari Posted January 23, 2010 Author Share Posted January 23, 2010 Thank you for the replies... I just want to echo $get_first_number, and when I do, it should give me 32 function extract_numbers($string) { preg_match_all('/([\d]+)/', $string, $match); return $match[0]; } $string = '32hello42good32morning'; Untill now, I have the above script... What happens after that? Can you complete it so $get_first_number will print 32 Link to comment https://forums.phpfreaks.com/topic/189557-array-little-problem/#findComment-1000546 Share on other sites More sharing options...
PHP Monkeh Posted January 23, 2010 Share Posted January 23, 2010 I already gave you a working answer. <?php function extract_numbers($string) { preg_match_all('/([\d]+)/', $string, $match); return $match[0]; } $string = '32hello42good32morning'; $numbers_array = extract_numbers($string); echo $numbers_array[0]; ?> Link to comment https://forums.phpfreaks.com/topic/189557-array-little-problem/#findComment-1000550 Share on other sites More sharing options...
npsari Posted January 23, 2010 Author Share Posted January 23, 2010 Monkeh, I am sorry, I did not see your previous script. I just saw the words. Thank you much. Yes, it works now. Good job friend. Link to comment https://forums.phpfreaks.com/topic/189557-array-little-problem/#findComment-1000552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.