Вопрос

I try to delete the vacuum line. My script, search a line, delete her, but it's remains a vacuum line.

hello
dude
world 

I did this script :

$chaine="dude"; 
$file=$filename; 
$texte = file_get_contents($file); 
$texte = str_replace($chaine ,'',$texte); 
$texte = preg_replace('/\s+/s', "\n", $texte);

try
    {

        if(!$fd = fopen($filename, "w")){
            throw new Exception("Erreur ouverture du fichier");
        }
    else
    {   
        fwrite($fd, $texte); 

        /*On ferme le fichier*/
        fclose($fd);    
    }

    }

    catch (Exception $e) {
        echo "Error de parametre fichier introuvable (File: ".$e->getFile().", line ".
        $e->getLine()."): ".$e->getMessage();
        echo "\n";
    }

And I have

hello
world

But if I delete the first one : hello, I have a vacuum line :

(vacuum line)
world

How can I fix the problem ?

I try trim, str_replace but it's doesn't work...

Это было полезно?

Решение

Replace this:

$texte = str_replace($chaine ,'',$texte); 
$texte = preg_replace('/\s+/s', "\n", $texte);

with this:

$texte = preg_replace('#' . $chaine . '\s*#', "\r", $texte);

I don't know how your linebreaks look like, but try with "\r", "\n" or "\r\n" as the replacement string.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top