Trim Function doesn't Work

1.5k views Asked by At

Hi (sorry for my english xD),

I'm taking data of my DB in MySql, this data (pair of players in format 'Player 1 - Player 2'). I cut this with explode function by character - therefore I get 'Player 1 ' and ' Player 2' string. I try delete white spaces at the beginning and the end with trim function, but this don't work. I suspect that this trouble is similar to other with the - character, the normal - of my keyboard don't split the string with explode function, I had to go to PHPMyAdmin and copy the - character of the one existing string and paste this in the second explode function parameter, after this, explode function work.

I try the same of the - character (Login in PHPMyAdmin copy one whitespace and paste this as second parameter of trim function but this time don't work :( ).

Also try with str_replace function (no matter what the gaps are eliminated) and don't work, also try with preg_replace function etc... nothing has work because apparently the white space saved in DB isn't the same of my keyboard, I suspect that the problem is related with charset's or collations but I don't have idea what I should do, because the table, link, charset of PHP and other thing are in UTF-8 and utf8-spanish-ci collation and charset.

Aclaration: The input of the data is made by xdom (PHP) from other site not controlled for me.

<tbody>
<?php while ($partidos = mysqli_fetch_array($sql)) { ?>
    <tr>
        <td class="center"><?php echo $partidos['fecha']; ?></td>
        <td><?php $participante = explode("₋", $partidos['jugadores']);
            $participante1 = trim($participante[0]);
            $participante2 = trim($participante[1]);

            echo trim($participante1) . "<br>vs<br>" . trim($participante2);
            ?></td>
        <td><?php if ($partidos['estado'] == 0) { ?> En Curso<?php } elseif ($partidos['estado'] == 1) {
                if ($partidos['alerta'] == 0) { ?> Terminado<?php } else { ?>Terminado - Error <i
                    class="fa fa-exclamation-triangle"></i><?php }
            } ?></td>
        <td><?php if ($partidos['alerta_medica'] == 1) { ?> Si <i class="fa fa-2 fa-medkit"
                                                                  style="color:#C00;"></i><?php } else { ?> No<?php } ?>
        </td>
        <?php if ($_SESSION['tipo'] == 1 || $_SESSION['tipo'] == 2) { ?>
            <td class="center">
            </td>
        <?php } ?>
    </tr>
<?php } ?>
</tbody>

Screenshot from command line

2

There are 2 answers

0
Jjsg08 On BEST ANSWER

I found the problem thanks to @Rick James, watching the hexadecimal response of the database i noticed that two spaces are x20, but one is xC2 xA0. Reading in other post of stackoverflow, i found that xC2 xA0 is a Non-break space, but this is not recognized for the trim function. The solution was specify this as second parameter of the function:

$participante1 = trim($participante[0],"\x20,\xC2,\xA0");
$participante2 = trim($participante[1],"\x20,\xC2,\xA0");

Now the whitespaces are removed correctly :3

Thanks to all for give your help to solve this :)

3
Alex Andrei On

Since you have 3 blanks on either side of the special character split the strings by 3 spaces.

like this:

<?php

foreach( $strings as $string) {
    $players = explode("   ",$string);
    print "Player 1: " . $players[0]  . PHP_EOL;
    print "Player 2: " . $players[2]  . PHP_EOL;
}