简体   繁体   中英

PHP replacing character in string

I tried to change character 'ç' with 'c' in a string using preg_replace() function. This segment of my code where i am trying to do it.

                 echo $mystr;  // The output of it is : çanakkale
                 $pattern = array("'ç'");
                 $replace = array('c'); 
                 $mystr = preg_replace($pattern, $replace, $mystr);
                 echo $mystr;  

This code works when i add before this line before the first line:

                 $mystr = "çanakkale";

However when I get this string from Database this code has no effect on it. How can I fix it? Any kind of help will be appreciated.

I got the answer there is nothing wrong with that code segment. But the reason why it doesnt changes anything is the charset of my database is ISO 8859-9 . Mapping this charset to UTF-8 will solve this.

  1. There is no need to use arrays here.

  2. Also, you have put an extra set of quotes inside your $pattern , which is causing the match to fail.

  3. Your pattern needs delimiters / .

      $mystr= 'çanakkale'; $pattern = '/ç/'; $replace = 'c'; $mystr = preg_replace($pattern, $replace, $mystr); echo $mystr; 

您需要使用u修饰符:

$mystr = preg_replace('/ç/u', 'c', $mystr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM