Recently i’ve switched to PHP 5.3+ and after that migration i learned that the eregi() function has been depreciated, its the function i mostly used for my regex needs.
But now i had to switch to preg_match() function, i am having trouble validating a certain condition.
Hello World
I want the preg_match to validate the above “hello world” string, the string contains whitespace in it.
But i want it to validate even if there is no whitespace “helloWorld” or just “hello”.
What i’m trying to do is that in my script i have a text-field for the category title, i want it to accept spaces in it and no other special characters (such as ._-+*,).
I have made it to work but i’m not sure if thats the right way which i’m using.
preg_match('/^[a-zA-Z0-9\s]*$/', $cat_name);'
Any help would be appreciated.
Looks OK, but will accept empty string, so you can change * to +
Remember that this regex will propably no accept language-specified UTF-8 chars.
See that comment in manual:
http://www.php.net/manual/en/function.preg-match.php#95828
Also note, that some specific charaters are very often used in titles, like ” and maybe it would better to allow it.