Remove special characters to a string in PHP?

0

How about? Veran I have a little problem with some strings that are full of a unicode character.

My string is this:

$variable = "c\u0000o\u0000d\u0000e";

Which has this unicode character: \ u0000 I would like to know how I can remove all these "\ u0000" and leave my string like this:

$formateada = "code";

That is, without all those "junk" codes.

    
asked by RolandF 09.04.2018 в 07:05
source

2 answers

1

a serious way to use str_replace, indicating as the first parameter the unicode (what you want to remove), as second a empty string (you substitute it by empty) and as third the string you have. It would be something like:

$cadena_limpia = str_replace('\u0000', '', $variable);
    
answered by 09.04.2018 / 08:53
source
1

See if this option works for you

<?php
$variable = "c\u0000o\u0000d\u0000e";

function func($s)
{
    $ret = '';
    for($i = 0; $i < strlen($s); $i++)
    {
        if($s[$i] == '\' && $s[$i+1] == 'u')
        {
            $i += 5;
        }
        else
        {
            $ret .= $s[$i];
        }
    }
    return $ret;
}
echo $variable;
echo "<br>";
echo func($variable);

?>
    
answered by 09.04.2018 в 09:14