Jump to content

Mb_Ereg_Replace Acting Differently Now It's Moved To A Function


Recommended Posts

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.

 

Original-1.png

 

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.

 

Original1.png

 

 

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.

new.png

 

Now though when it passes into the function it starts out ok, i.e. the content of $input is still the same.

new3.png

However, once I step over that line of code and $input goes through mb_ereg_replace the content is changed as below.

new2.png

 

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.