1

Hi how to format a string from unwanted characters in php? Tried strip_tags, mysqli_real_escape_string, but no success.

This is the scenario. The string is entered through an android app it allows rich text inputs, below is the image enter image description here

When added the db entry looks normal, below is the image

enter image description here

Now at last the string is read from a url, below is the image

enter image description here

As you can see there's  character between the bold text, but it's not appearing in the db entry on second image. Actually this string is read by a gsm module and it's causing the system to break due to the  character.

strip_tags returns the same string, whereas mysqli_real_escape_string returns empty string.

How to clean such strings from unwanted characters(not just Â) in php to return only the plain text?

coderSree
  • 329
  • 1
  • 12
  • Can you show what that string looks like in your PHP code? Like the var_dump output of $_GET or $_POST or wherever it ends up? – Don't Panic Mar 21 '19 at 20:47

1 Answers1

0

It has to do with Unicode vs. ASCII. your locale must be set to ASCII before the code will work since the [[:^print:]] reference determines what is not printable based on the current locale. This is the post that has the answer for both strings and arrays:

Remove non-ascii characters from string

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

And another reference with a good explanation: https://alvinalexander.com/php/how-to-remove-non-printable-characters-in-string-regex