earmsby Posted February 4, 2009 Share Posted February 4, 2009 Hi, I'm a bit stumped on this one and it's possible this should really go in the REGEX forum but I'm not sure. Here's what I'm trying to do: I have a string/word that may have special characters within it (ex. Andrée). I want loop through the word and compare each letter with a list of special characters and a replacement letter that is a non-special character (ex. é would be replaced by e). In the process of trying to figure out how to do this, I made a little script that loops through the word and puts each letter in an array. To my surprise, there are more elements in the array than I expected. The accented e seems to take two spots in the array. When I print_r the array, I get: Array ( [0] => A [1] => n [2] => d [3] => r [4] => � [5] => � [6] => e ) So, I'm confused about how PHP is interpreting the special character. I tried to read in both the PHP online documentation as well as a PHP programming book I have about this but nothing I read really clarified for me what was going on with this. Can anyone help me understand how to approach this seemingly simple problem? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/ Share on other sites More sharing options...
trq Posted February 4, 2009 Share Posted February 4, 2009 No need to break the string into chars. Just use str_replace over the entire string. eg; $string = str_replace(array('é'), array('e'), $string); You can add as many elements to each array as you need. Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/#findComment-754749 Share on other sites More sharing options...
earmsby Posted February 4, 2009 Author Share Posted February 4, 2009 Thanks! That's much simpler than I was making it into. I'll play with that and hopefully it will do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/#findComment-754761 Share on other sites More sharing options...
earmsby Posted February 19, 2009 Author Share Posted February 19, 2009 It's been a little while since I posted this question and received the response from thorpe, so my apologies for resurrecting an old thread. The suggestion worked fine as long as I manually supplied the array of substitution characters into the PHP code. However, when I tried to pull the array from a MYSQL database the characters don't seem to be the same. The letters/character were added to the DB using php code by simply pasting the character into a form which runs an INSERT query into the table of letter substitutions. There must be some encoding issues between MYSQL and PHP that I'm not understanding. ??? Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/#findComment-766525 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 MySQL has its own charset, as well as the html that may have been used to post it to. What is the charset your MySQL database is set to run? Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/#findComment-766532 Share on other sites More sharing options...
earmsby Posted February 19, 2009 Author Share Posted February 19, 2009 Latin1 but I also tried utf to no avail. I may end up just hard coding the substitutions if I can't make it work with the database but I'd still be interested in know how for other situations where I need to work with database encoding and php. Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/#findComment-766537 Share on other sites More sharing options...
earmsby Posted February 19, 2009 Author Share Posted February 19, 2009 Doh! I just solved my own problem... since the replacement characters will be run against another table in the same database, the character encoding is not a problem. We are comparing apples to apples. The problem was when I was either using characters from the database compared against characters typed in or characters hard-coded in against words in the database. Quote Link to comment https://forums.phpfreaks.com/topic/143838-solved-finding-special-characters-in-string/#findComment-766580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.