MIRMIX
MIRMIX

Reputation: 1080

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.

Upvotes: 0

Views: 1819

Answers (3)

MIRMIX
MIRMIX

Reputation: 1080

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.

Upvotes: 2

merlin2011
merlin2011

Reputation: 75555

  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; 
    

Upvotes: 1

Sharanya Dutta
Sharanya Dutta

Reputation: 4021

You need to use the u modifier:

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

Upvotes: 0

Related Questions