0

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.

4
  • Your method seems to work fine: codepad.org/ykGhxFPd Commented Jul 11, 2014 at 6:52
  • Then why it gives same output? Commented Jul 11, 2014 at 6:56
  • Why do not use str_replace in your case. To my mind, Perform a regular expression with preg_replace is useless / heavy to use just to replace only one letter. Anyway it won't solve your problem... Commented Jul 11, 2014 at 7:13
  • I gonna convert string of array Commented Jul 11, 2014 at 7:15

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
  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; 
    

Comments

0

You need to use the u modifier:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.