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"; 
18
  • You just want to delete them and not replace them with something? Commented Nov 1, 2015 at 13:14
  • @lurker ya justwant to delete those characters. Commented Nov 1, 2015 at 13:16
  • 2
    Possible duplicate of Regular Expression Sanitize (PHP) Commented Nov 1, 2015 at 13:19
  • @lurker No. My question is different. Check the example string in my question. And expected output. (See the Edit) Commented Nov 1, 2015 at 13:31
  • What about this question? 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". Commented Nov 1, 2015 at 13:34

2 Answers 2

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

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.