Jump to content

Array Little Problem!


npsari

Recommended Posts

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

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

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];

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.