Forum Moderators: coopster
I have tried a few variants of the following but nothing seeems to work.
if(!preg_match('[^A-Za-z0-9]+', $line[name])){
print $line[name];
}
Any ideas what's wrong?
Thanks,
if(preg_match('/[a-z0-9]+/i', $row)){
print $row;
}
Don't forget that PHP offers us preg_quote [php.net] as well.
MrSpeed you might have a problem with that regex (above). Within the [] if you want a - to be treated as an actual - it has to be at the start of the pattern (or after the ^ in this case).
I'll have to play around a bit with your suggestion. In a nutshell I want to display the fields if it only contains letters,numbers,dashes and maybe spaces.
Very much right. The hyphen indicates character range, just like in the earlier part of your pattern,
a-z. So, in your current pattern, you are telling it to match any letter from a to z, any number from 0 to 9, and any character between space and the slash. Have a look at the ascii table [asciitable.com] and see that you will allow plenty of other characters within that range, including
!"#$%&'()*+,-.. My tests have shown that the hyphen doesn't necessarily have to be at the beginning of your pattern, but it sure does keep things tidier for you and helps reduce the possibility of you incorporating the hyphen in a range. Both work:
"/[^a-z0-9- \/]/i"
or
"/[^-a-z0-9 \/]/i"
If a minus character is required in a class, it must be escaped with a backslash or appear in a position where it cannot be interpreted as indicating a range, typically as the first or last character in the class.