Forum Moderators: coopster
The value of $text: “20 years old”
I need to filter the text and get the number “20” so I know the age of a user. How can I filter the value of $text and get the first word or digits in a sentence. The words are separated with spaces.
Anybody kind enough to help me end my misery and help me lift my gloom?
Thank you
M
$text = "21 years old";
preg_match("/^([0-9]+)/i",$text, $matches);
$age = $matches[1];
print "The age is $age.";
keep in mind that the ^ indicates that the string will be starting with a number! If the number is not always going to be the very first thing in the string....take the ^ out.