0

I have a string as shown below

$string1="then & add â...“ to";

The special ascii characters â “ Â etc. causing errors.

So i want to know there is any default function or ways to remove such characters?

expected output after processing :

$string1="then & add … to";
  • You just want to delete them and not replace them with something? – lurker Nov 01 '15 at 13:14
  • @lurker ya justwant to delete those characters. – user5512556 Nov 01 '15 at 13:16
  • 2
    Possible duplicate of [Regular Expression Sanitize (PHP)](http://stackoverflow.com/questions/3022185/regular-expression-sanitize-php) – lurker Nov 01 '15 at 13:19
  • @lurker No. My question is different. Check the example string in my question. And expected output. (See the Edit) – user5512556 Nov 01 '15 at 13:31
  • What about [this question](http://stackoverflow.com/questions/14114411/remove-all-special-characters-from-a-string)? I would think a minor tweak of what's in those other questions would solve your problem. It is basically the same problem. Also, in your example, what's `...`? There is a special character that represents ellipses, but I don't know if that's what you mean or just showing "other characters go here". – lurker Nov 01 '15 at 13:34
  • Why you want to leave dots, since it's also a non-printable character? see https://regex101.com/r/tC9uX4/1 – Avinash Raj Nov 01 '15 at 13:36
  • The issue actually not about url encryption. I don't want to remove normal special characters like &, ! etc. I just want to remove characters like â “ Â etc. – user5512556 Nov 01 '15 at 13:36
  • Maybe it would be better if you sort out those errors instead of sanitizing? – Kate Miháliková Nov 01 '15 at 13:38
  • @KateMihalikova those characters causing the errors. Hope u understand – user5512556 Nov 01 '15 at 13:42
  • And you can resolve those errors instead of removing these characters :) Basically there are almost no unresolvable errors thrown from string processing. – Kate Miháliková Nov 01 '15 at 13:43
  • Oh my god i can't resolve those errors without removing those characters. Because those characters are the reason for errors. – user5512556 Nov 01 '15 at 13:45
  • And can you share those errors with us? – Kate Miháliková Nov 01 '15 at 13:45
  • The error is occurring when am trying to read those strings from my mysql database. Its actually not displaying anything(just the result : null). With out those characters the code works well. @KateMihalikova hope u understand. – user5512556 Nov 01 '15 at 13:48
  • Did you check that the string is saved properly in the database? Seems like you have not properly set encoding on mysql's end. – Kate Miháliková Nov 01 '15 at 13:52
  • @KateMihalikova i finally found a solution. check my answer. Thanks for all of ur attempts. – user5512556 Nov 02 '15 at 04:26
  • I think that you solved the wrong part of your code - now billions of people can't use your app because you are stripping characters they use even in their names.. – Kate Miháliková Nov 02 '15 at 04:48
  • @KateMihalikova I don't think. Because I never seen any such names in my life. :P – user5512556 Nov 02 '15 at 06:52
  • @user5512556 Even my official name includes ř and my surname includes á. Or my katakana-transcribed name: ケイト ミハーリコヴァー. But of course that is your problem. I'm just pointing out for others that this kind of problem shouldn't be resolved by removing or replacing characters. Everytime I'm designing anything with user input, especially with names, I reread [that article](http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) about wrong assumptions of programmers about names. – Kate Miháliková Nov 03 '15 at 13:20

2 Answers2

1
$str = 'aAÂ';
$str = preg_replace('/[[:^print:]]/', '', $str);

This is the solution i wanted. Thanks everybody for ur attempts for help me. remove non-ascii characters from string in php

Community
  • 1
  • 1
0

You will need mb_string installed and enabled in php.ini (which it is by default now). On centos install the php-mbstring package and restart the web server, if running via web.

<?php

$string = "aâ";

print $string . "\n";

$length = mb_strlen( $string );
$index = 0;

$output = '';

while( $index < $length )
{
   $char = $string[$index];

   if( mb_check_encoding( $char, 'ASCII') )
   {
      $output .= $string[$index];
   }
   $index++;
}

print $output . "\n";
?>

Result:

aâ
a

For replacing the character with underscore, you can modify the code to append '_' to the string if check encoding does not return 1.

http://php.net/manual/en/book.mbstring.php

the happy mamba
  • 468
  • 2
  • 6