![]() |
Using regex
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?
PHP Code:
|
Use this range
[!-@]This is because ranges go by ASCII value, where ! is the first punctuation (after space) and @ is the last. |
PHP Code:
|
As a side note, it's best just to stop using the ereg_* function and it's POCL ( I think it is ) engine because as of 5.3.0 it's going to through an E_DEPRECIATED error. Use the preg_* family
|
Thanks for the suggestions but they don't seem to be working. I changed the input string to more properly illustrate the problem. So given this string
PHP Code:
Quote:
PHP Code:
Quote:
PHP Code:
Quote:
|
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 PHP Code:
PHP Code:
|
PHP Code:
|
My thanks to everyone. The code is working as expected now and I have a better understanding of how to manipulate the string. I do appreciate it.
|
| All times are GMT. The time now is 01:18 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0