Forum Moderators: coopster

Message Too Old, No Replies

how to retrieve words in a sentence?

20 years old. How can I filter the string for the "20"

         

Mitch888

4:58 am on Sep 6, 2003 (gmt 0)

10+ Year Member



Hi,
This is probably the most stupid question one can ever ask, but after exhausting all known resources I know of, I reached to state of desperation and cannot help feel stupid. Well here it goes:

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

AprilS

5:12 am on Sep 6, 2003 (gmt 0)

10+ Year Member



Hope this helps you out! :D

$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.

Mitch888

6:04 am on Sep 6, 2003 (gmt 0)

10+ Year Member



thank you thank you thank you from the bottom of my heart. IT WORKS :)

Now I can go and get some sleep and try to get rid of my raccoon eyes :)

thank you