PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I currently use ereg_replace() to remove the spaces from an article title and replace them with dashes to be used in a URL. My question is what is the best method to remove all non alpha/numeric characters such as ", !, #, '? Should I store them in an array and use that, i.e. ereg_repalce($badcharacters, '') or is there a better method?
Generally speaking, you should favor the PCRE functions (preg_replace, preg_split, preg_match) over the EREG functions because they're faster and binary-safe.
Why not try splitting the string on "bad characters" and then imploding it with a dash as the glue?
That works too. If you match dashes, the compression will happen automatically. The only thing is you won't be able to get rid of trailing dashes with that method (easily, maybe some regexp-fu will make it happen)