check if a string starts with an integer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

check if a string starts with an integer

Post by runelore »

Hi I wanna check if a string starts with an integer, I know I need to use a pregnatch in some way but for the life of me I cant seem to get it right, anyone help? thanks....
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post by runelore »

sorry, check if a string starts with a number.. Sort of got the jargon mixed up there. : )
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try something like:

Code: Select all

$string = '12something';
if (preg_match('/^[0-9]/', $string)) {
    // string starts with number
} else {
    // string doesn't start with a number
}
Mac
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post by runelore »

Thank you very much, that is exactly what I needed to work with, thanks again..
Post Reply