Copenhagener;10933747 wrote:Still not working 100%, but close...
if(!preg_match("/^[0-9A-Za-zÆØÅæøå ,;-\?\!\.\:\"]+$/",$string)) { ...
The / and " do not get through.
I cannot figure out how to implemet the preg_quote on these two characters. How do I escape these ones?
A couple of things to note.. first, most metacharacters loose their meaning within a character class [...]. So characters like the dot and questionmark don't require escaping. The dash is potentially another story though...if it is not the very first or very last character in the character class, and it is not escaped, it will create a range. So in this case, you created a range from ; to ?
So your pattern could be cleaned up a tad:
if(!preg_match('#^[0-9a-zÆØÅæøå\s,;?!.:"/-]+$#i', $string)) { ...
What I have done here is a couple of things:
1) Used single quotes instead of double quotes outside the pattern.
2) Changed the delimiters from / to #, that way, you won't have to escape the / inside the character class.
3) Added the 'i' modifier after the closing delimiter. This makes things case insensitive, so it will match a-z and A-Z.
4) placed the dash at the very end (thus no need to escape it, and it doesn't create a range).
5) Unescaped stuff like ?, !, . etc...
6) I added the character class shorthand \s instead of your literal space (\s represents all whitespace characters, like space, tab, newlines, carriage returns, etc..)
Hopefully that all helps.