John Davidson

php - str_ireplace not work with utf-8 characters

0 comments
Message:


I use str_ireplace (not str_replace) to replace some words, but it doesn't seem to work with utf-8, e.g. "đ".


There are any solution for this issue?


Many thanks.


Example input


 str_ireplace("đ", "b", "bĐb");


My Answer:
special characters like accented letters or emojis.

To replace words with utf-8 characters, you can use the mb_eregi_replace function instead. This function is similar to str_ireplace but works with multibyte characters, including utf-8 characters.

Here's an example of how you can use mb_eregi_replace to replace words with utf-8 characters:

php
$text = "This is a café.";
$replaced_text = mb_eregi_replace("café", "coffee shop", $text);

echo $replaced_text;


In this example, the word "café" is replaced with "coffee shop" using mb_eregi_replace. This function should work correctly with utf-8 characters and special characters.

Rate this post

5 of 5 based on 4354 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us