How to make this function using preg or preg_match ?

I’m new to php n’ doing some form verification stuff. So I came across this preg function and used it. But it issues "Function ereg() is deprecated " error. and in PHP manual it says that we can use preg() instead of ereg(). But I don’t understand what is says and it seems really difficult. please help me to solve this. this is a part of my code which use ereg().

[php]
if (ereg(’^[A-Z][- a-zA-Z]+$’,$var)) {
$valid = true;
}
[/php]

Please kindly help me

I adjusted the pattern to work with preg_match (added delimiters), added a $var=‘John Doe’ for testing and echo the true or false to verify. You can remove the echo’s if you need to.

Basically this will return true if the value of $var begins with a capital letter followed by a {space} a {dash} or a upper or lower case letter.

Example:
[php]
$var = ‘John Doe’;
if (preg_match(’~^[A-Z][- a-zA-Z]+$~’,$var)) {
$valid = true;
echo ‘true’;
} else {
echo ‘false’;
}
[/php]

Hope this helps.

Thank you so much dear friend , I was looking all over for get an answer for this and here you are. Thanks again for your kind reply

Sponsor our Newsletter | Privacy Policy | Terms of Service