regex - php preg_match() code for comma separated names -
i need validate input patterns using preg_match()
patterns " anyname1,anyname2,anyname3, ".
note there comma @ end too. letter or number valid between commas, numbers not have appear @ end. e.g "nam1e,na2me,nnm22," valid.
i tried ^([a-za-z0-9] *, *)*[a-za-z0-9]$
did no work. have gone through other posts did not perfect answer.
can give me answer this?
it sounds want validate string contains series of comma separated alpha-numeric substrings, optional trailing comma.
in situation, should achieve want.
$str = "anyname1,anyname2,anyname3,"; $re = '~^([a-z0-9]+,)+$~i'; if (preg_match($re, $str)) { // string matches pattern! } else { // nope! }
if value stored in $str
contains trailing space in example, , don't want use trim() on value, following regex allow whitespace @ end of $str
:
~^([a-z0-9]+,)+\s*$~i
Comments
Post a Comment