5

I have clean function for remove special caracter from string but that function also removing Turkish caracter (ı,ğ,ş,ç,ö) from string

function clean($string) {
   $string = str_replace(' ', ' ', $string); 
   $string = preg_replace('/[^A-Za-z0-9\-]/', ' ', $string); 

   return preg_replace('/-+/', '-', $string); 
}

How can I fix it ?

2
  • 2
    It's doing exactly what you told it to - removing any characters that aren't A-Z, a-z, 0-9 or -. Try telling it not to remove letters, eg. \p{L} Commented Mar 20, 2016 at 17:01
  • For example : INPUT TEXT : Lisede sınıf ar'kad'aşım OUTPUT : Lisede s n f arkada m I need : Lisede sınıf arkadaşım Commented Mar 20, 2016 at 17:08

5 Answers 5

5

Add those characters you want to keep to preg, also add Upper cases if neededç I edited your code:

function clean($string) {
    $string = str_replace(' ', ' ', $string);
    $string = preg_replace('/[^A-Za-z0-9\-ığşçöüÖÇŞİıĞ]/', ' ', $string);

    return preg_replace('/-+/', '-', $string);
}

Test:

$str='Merhaba=Türkiye 12345 çok çalış another one ! *, !@_';
var_dump(clean($str));
//Output: string(57) "Merhaba Türkiye 12345 çok çalış another one   "
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you my friend you save me from big troble
2

You can use iconv to replacing special characters like à->a, è->e

<?php
    $string = "ʿABBĀSĀBĀD";
    echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $string);
    // output: [nothing, and you get a notice]
    echo iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
    // output: ABBSBD
    echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
    // output: ABBASABAD
    // Yay! That's what I wanted!
    ?>

Credits:

https://gist.github.com/swas/10643194

@dmp y @Nisse Engström

Comments

1

Maybe you can try:

function clean($string) {
   $string = str_replace(' ', ' ', $string); 
   $string = preg_replace('/[^A-Za-z0-9ĞİŞığşçö\-]/', ' ', $string); 

   return preg_replace('/-+/', '-', $string); 
}

Comments

1

Which special characters you want to replace? Maybe be it'll be easier to change a paradigm of cleaning from everything except ... to something concrete.

<?php

function garbagereplace($string) {

$garbagearray = array('@','#','$','%','^','&','*');
$garbagecount = count($garbagearray);
for ($i=0; $i<$garbagecount; $i++) {
$string = str_replace($garbagearray[$i], '-', $string);
}

return $string;
}

echo garbagereplace('text@#$text%^&*text');

?>

Comments

0

You could use the following to remove accents from accented characters.

function removeAccents($subject){
  //Note:  This is not a complete list of accented characters
  $accented = array('ü','Ü','ú','È','É','Ê','Ë','é','ê','ë','è','£','Ğ','İ','Ş','ı','ğ','ş','ç','ö','Ÿ','ÿ','á','ñ','Ñ','À','Á','Â','Ã','Ä','Å');
  $clean = array('u','U','u','E','E','E','E','e','e','e','e','f','G','I','S','l','g','s','c','o','Y','y','a','n','N','A','A','A','A','A','A');

  $accentcount = count($accented);
  for ($i=0;$i<$accentcount;$i++){
    $subject = str_replace($accented[$i], $clean[$i], $subject);
  }
  return $subject;
}

For example:

removeAccents("el niño");

Returns "el nino"

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.