Jyothish V
Jyothish V

Reputation: 76

PHP unknown character in string

While calling an HTTP API i get a string of integers as result. the var_dump of the same is like below

string '249139110' (length=12)

But this string have some invisible characters along with it. I tried the below code

foreach (count_chars($id, 1) as $i => $val) {
   echo "<br>There were $val instance(s) of \"" , chr($i) , "\" in the string." . ord($i) . " : " .gettype($i) ;
}

and the result i like below

There were 1 instance(s) of "0" in the string.52 : integer
There were 3 instance(s) of "1" in the string.52 : integer
There were 1 instance(s) of "2" in the string.53 : integer
There were 1 instance(s) of "3" in the string.53 : integer
There were 1 instance(s) of "4" in the string.53 : integer
There were 2 instance(s) of "9" in the string.53 : integer
There were 1 instance(s) of "�" in the string.49 : integer
There were 1 instance(s) of "�" in the string.49 : integer
There were 1 instance(s) of "�" in the string.50 : integer

Please help me to remove these � characters. Thanks in advance.

Upvotes: 0

Views: 495

Answers (1)

Saad Suri
Saad Suri

Reputation: 1382

FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH and FILTER_UNSAFE_RAW will do the trick Try the code below

$unsafe='There were 1 instance(s) of "�" in the string.49 : integer';
$string = filter_var($unsafe, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
echo $string;

Output will be:

There were 1 instance(s) of "" in the string.49 : integer

For more information read: String sanitization through filter_var

Upvotes: 1

Related Questions