timski72 Posted December 8, 2012 Share Posted December 8, 2012 Hi Guys, I was using mb_ereg_replace in a script. It was using it to convert Greek characters to their latin ahplabet equivalent. That was working fine. Then I moved it to a function in a new script and now it doesn't seem to work. The following lines of code: $input = "η φιλι"; $input = mb_ereg_replace("τζ", "dz", $input); used to leave $input untouched as "τζ" doesn't appear in $input. However when I moved this to a function $input was changed by this line of code. I've pasted some screenshots that I made whilst debugging this in phpDesigner to show what I mean. OK this is a little difficult to show as the Greek characters show as goggledegook in the debugger but the first screenshot shows that I set $input to "η φιλι" which shows as I've highlighted in yellow in the screenshot below in the debugger. If you echo $input it shows as "η φιλι". I'm not sure why it shows as highlighted in yellow below in the debugger but that's not the problem. I presume phpDesigner doesn't display unicode properly. Anyway, once the content of $input has passed through mb_ereg_replace within the same script you can see the content has remained the same (see below). This is as expected as "τζ" isn't present in $input. However, once I moved this code to a new script and put it in a function it behaved differently (see below). As you can see I've set $input to "η φιλι" again and that is the same as above. Now though when it passes into the function it starts out ok, i.e. the content of $input is still the same. However, once I step over that line of code and $input goes through mb_ereg_replace the content is changed as below. As "τζ" doesn't appear in $input I would expect it to remain unchanged as it did when this line of code was in a single script. I don't understand why this has changed by moving it to a new script and function? Any help would be very welcome. Thank you, Tim. Quote Link to comment https://forums.phpfreaks.com/topic/271747-mb_ereg_replace-acting-differently-now-its-moved-to-a-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2012 Share Posted December 8, 2012 For the code that doesn't work, have you set the mb_internal_encoding anywhere in the script? Quote Link to comment https://forums.phpfreaks.com/topic/271747-mb_ereg_replace-acting-differently-now-its-moved-to-a-function/#findComment-1398224 Share on other sites More sharing options...
timski72 Posted December 8, 2012 Author Share Posted December 8, 2012 Yes. I hadn't at the time that I made the screenshot but now the code includes this line mb_internal_encoding("UTF-8"); but still the content is being 'corrupted'. Thanks, Tim. Quote Link to comment https://forums.phpfreaks.com/topic/271747-mb_ereg_replace-acting-differently-now-its-moved-to-a-function/#findComment-1398233 Share on other sites More sharing options...
Christian F. Posted December 9, 2012 Share Posted December 9, 2012 Verify that the file is indeed saved as UTF-8, and that your editor/debugger is treating it as such. Not to mention HTTP headers, and everything else involved, of course. Also, why are you using two mb_strtolower () calls after each other, on the same variable? Doesn't make a whole lot of sense, to be honest. A further improvement you could do is to replace mb_ereg_replace () with a straight str_replace () (it works with multi-byte strings), seeing as you're not using an actual Regular Expression in your replacements. Quote Link to comment https://forums.phpfreaks.com/topic/271747-mb_ereg_replace-acting-differently-now-its-moved-to-a-function/#findComment-1398326 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.