Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP- trim() function not working

Tags:

php

trim

I have problem when used function trim() with this code

$handle = @fopen("55.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {      
        $d = explode(" ", $buffer);
        foreach($d as $val) { 
            echo '<br>'.trim($val,'.');  //why not work 
        }   
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }   
    fclose($handle);
}

The trim() doesn't trim '.'.

like image 380
user201635 Avatar asked Nov 24 '12 22:11

user201635


1 Answers

Additional to the lines themselves, the fgets() function returns the line breaks from the file, which are after the dots in the string, thus preventing the dots from being trimmed, because they are not actually the last character.

Try to trim the dots and possible line breaks at the same time:

echo '<br>'.trim($val, ".\r\n");
like image 180
Wolfgang Stengel Avatar answered Oct 06 '22 01:10

Wolfgang Stengel