The following code, the way I understand it, is supposed to strip all punctuation characters from the string. From what I've found on the web, a space is not one of those characters. But the code below outputs "abstractmodern." Would someone please explain how I can strip all special characters except the spaces?
The #!S are all listed as characters in :punct: so those should be stripped. The first method does that but it also removes the hyphen. The second doesn't remove the punctuation but handles the hyphen correctly. Would someone point out my mistake, please?
Do you want to use a whitelist or a blacklist. The former would remove all characters except a specific set, e.g. allow only alphanumeric and hyphen characters. The latter, a blacklist, would remove only specific characters, e.g. !@#$%^&*().
It is also advised to use the PCRE functions (preg_replace) since the POSIX family of functions (ereg_*, split, etc.) is deprecated.
With regards to why ereg_replace("[[!-@],[:punct:]]","",$str) won't work, it's just how you've constructed the character set. There shouldn't be a set of square brackets around !-@. Note that that range includes the dash/hyphen character - so that will be removed.
Back to white- and blacklists, here are a few examples: Only allow alphanumeric (case insensitive) and hyphen characters