1

I have this function that strips illegal characters. I found it here http://php.net/manual/en/function.strtr.php, function fixoutput($str)

So, this is my code.

<?php 

$stuff = 'Foo ◻◻◻◻◻◻◻◻◻◻◻◻';


function fix_output($str){
    $newstr = '';
    $good[] = 9;  #tab
    $good[] = 10; #nl
    $good[] = 13; #cr
    for($a=32;$a<127;$a++){
        $good[] = $a;
    }
    $len = strlen($str);
    $strs = array();
    for($b=0;$b < $len+1; $b++){
        if(in_array(ord($str[$b]), $good)){
            $newstr .= $str[$b];
        }//fi
    }//rof
    return $newstr;
}


echo fix_output($stuff);
echo '<br>'.$stuff;

And I have this output.

Notice: Uninitialized string offset: 40 in /<directory>/foo/foo.php on line 17
Foo 
Foo ◻◻◻◻◻◻◻◻◻◻◻◻

I want a fix for this notice.

I'm having this notice because is $str a string in this context, not an array. I am are trying to treat it like an array, which doesn't work. I'm having trouble in creating a fix for this, could you guys lend me some ideas? Thanks a bunch! Please mention the things that are unclear.

yowza
  • 260
  • 2
  • 14
  • PHP and Unicode are not friends. `strlen("私")` is `3`. – Amadan Feb 16 '16 at 03:44
  • Ahh so I have to change `strlen` into other, right? – yowza Feb 16 '16 at 03:45
  • 2
    No, you need to make your code handle Unicode. Here's a start: [How to get a code point number for a given character in a UTF-8 string](http://stackoverflow.com/questions/395832/how-to-get-code-point-number-for-a-given-character-in-a-utf-8-string); and you can use `mb_strlen` and `mb_substr` to identify separate characters. – Amadan Feb 16 '16 at 03:47
  • Thank you for providing me details @armadan! This will help me! – yowza Feb 16 '16 at 03:49
  • 2
    Actually, ignore that - stripping everything outside `32..127` should also incidentally strip non-ASCII Unicode characters. Your problem seems to be counting till `$b < $len + 1`, which is one too much. – Amadan Feb 16 '16 at 03:51
  • @armadan I removed the `+1` and the notice was gone. Thanks for noticing it! – yowza Feb 16 '16 at 03:53

0 Answers0