On my website, I let the user send tags like this:
tag tagtwo anothertag
I only want to allow them to use single spaces and letters, so I want to remove numbers, dashes, double spaces, etc. So an invalid string would be:
tag tag2 another-tag
I have the following code to do this, but I don’t know the correct regex to use.
$tags = strtolower($_POST['imgTags']);
$tags = preg_replace("/regex/", "", $tags);
$tagArray = explode(" ", $tags);
What is the correct regEx to do this?
Also, I might want to replace é and ö with e and o.
This will remove anything but letters and spaces:
Then this will collapse consecutive spaces:
If you want to allow other kinds of whitespace characters, but also replace them with single spaces, try this instead:
Regarding your last sentence: there is no generic way to this. You will have to add specific rules. However,
preg_replace_callbackmight help you in determining the unmodified letters.