0

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.

2
  • 1
    Do a bin2hex to see what those are. (Perhaps NUL chars). Then trim, cast or regex-whitelist the string to an integer. Commented Jan 12, 2018 at 6:56
  • 2
    Trim the scring. Even the var_dump shows that it's longer than it is. PS: also you can output $i and look it up in ASCII table, e.g. here ascii.cl Commented Jan 12, 2018 at 6:59

1 Answer 1

1

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

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

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.