I'm looking to use some rewritten URLs which gives the title of a page. Unfortunately this can use a lot of symbols and accented letters due to the site being in a foreign language. How would I use preg replace to only allow alpha-numeric values and convert accents such as Ã, á, É, é, Ã, Ã, Ó, ó, Ú, ú, Ñ, ñ into their alphabetical equivalent? I've always been a bit iffy with preg replace and normally bow out and just write a few str_replace lines of code but I feel now is the time to stop being lazy and try and find out how to use the more complex preg replace function.
the two functions works in the same way, you can read more about preg_replace at http://www.php.net/manual/en/function.preg-replace.php You have there a lot of examples
Lets say the string was $title = "¿El éxito?"; How would I turn this into $title = "el-exito"; with the fewest possible lines of code?
Loop over each character in the title and check if it is one of the following ctype_alnum() ctype_punct() ctype_space() ctype_xdigit() ctype_cntrl() if none if these returns true, then delete that character.
Interesting method to use legend. Isn't there an easier way to do it with preg_replace though? I'm almost certain there's a way of removing all symbols from a string using preg_replace. It uses something like [a-zA-Z0-9] in the same sort of format as you get with htaccess.
$title = ereg_replace("[^ -_0-9a-z]","",$title); Apparently this piece of code removes all characters which are non-alphanumeric and are not lower case and do not contain symbols - and _ Just add A-Z after a-z if you want to allow upper case characters.
@Weirfire If you are still looking for the code to convert ""¿El éxito?" to "el-exito", here is the code. http://us2.php.net/manual/en/function.strtr.php#54635