在PHP中读取文件时,仅从段落开头删除空间

在PHP中读取文件时,仅从段落开头删除空间,php,file-handling,removing-whitespace,Php,File Handling,Removing Whitespace,朋友们好,我正在阅读一个文件夹,其中有许多PHP文本文件,我需要删除文本文件中每个段落开头的所有空格。但当我读取文件并试图删除空格时,它不起作用,就像我尝试了TRIM函数LTRIM和REGEX“/\s+$/”一样,但都不起作用。但是当我使用正则表达式时,它会删除所有的空格。我只需要删除段落开头的空格。请提供任何帮助。下面是我正在尝试的代码 if ($dh = opendir($folder_path)){ while (($file = readdir($dh)) !== false)

朋友们好,我正在阅读一个文件夹,其中有许多PHP文本文件,我需要删除文本文件中每个段落开头的所有空格。但当我读取文件并试图删除空格时,它不起作用,就像我尝试了TRIM函数LTRIM和REGEX“/\s+$/”一样,但都不起作用。但是当我使用正则表达式时,它会删除所有的空格。我只需要删除段落开头的空格。请提供任何帮助。下面是我正在尝试的代码

if ($dh = opendir($folder_path)){
    while (($file = readdir($dh)) !== false) {
        $path_parts = pathinfo($file);
        if ($path_parts['extension'] === 'txt' && $file1 = fopen($folder_path . '/' . $file, "r")) {
            $cleanStr ="";
            while(!feof($file1)) {
                $line = trim(fgets($file1));
                if ( trim($line)!="" ){
                    $str = preg_replace("/\s+$/", " ", ltrim($line));
                    echo $str;die;
                }
            }
        }
    }
    echo "Process finished..." . PHP_EOL;
    closedir($dh);
}

使用
/^\s+/
匹配段落开头的空格。例如

$str = preg_replace("/^\s+/", "", $line);

几年前,我在一个类似的任务中使用了类似的东西-似乎工作正常,也许它会有用-但是如果你不想编辑文件而不是使用
file\u put\u contents
简单地
echo introde(PHP\u EOL,$content)

<?php

    $folder_path='c:/wwwroot/content/stack';



    /* find all files in chosen folder */
    $col=glob( $folder_path . '\*.*' );

    /* filter only text files for processing */
    $col=preg_grep( '@(\.txt$)@i', $col );

    /* Proceed if there are text files only */
    if( !empty( $col ) && count( $col ) > 0 ){

        /* iterate through collection */
        foreach( $col as $index => $filepath ){

            /* read file contents into an array */
            $lines=file( $filepath );

            /* store altered lines in temp array */
            $content=array();

            /* trim the start of each line & store into temp array */
            foreach( $lines as $line )if( !empty( $line ) ) $content[]=ltrim( $line );


            /* output file contents */
            file_put_contents( $filepath, implode( PHP_EOL, $content ) );
            echo file_get_contents( $filepath ) . PHP_EOL . PHP_EOL;
        }
        printf( "finito! %d files processed",count( $col ) );
    } else {
        echo "nothing to process";
    }
?>
以上大多数行的开头都有空格或制表符

<?php

    $folder_path='c:/wwwroot/content/stack';
    $trim='rtrim';


    /* find all files in chosen folder */
    $col=glob( $folder_path . '\*.*' );

    /* filter only text files for processing */
    $col=preg_grep( '@(\.txt$)@i', $col );

    /* Proceed if there are text files only */
    if( !empty( $col ) && count( $col ) > 0 ){

        /* iterate through collection */
        foreach( $col as $index => $filepath ){

            /* read file contents into an array */
            $lines=file( $filepath );

            /* store altered lines in temp array */
            $content=array();

            /* trim the start of each line & store into temp array */
            foreach( $lines as $line )if( !empty( $line ) ){
                $content[]=$trim( $line );
            }

            /* write content back to file */
            #file_put_contents( $filepath, implode( PHP_EOL, $data ) );
            echo "<pre>" . implode( PHP_EOL, $content ) . "</pre>";
        }
        printf( "finito! %d files processed",count( $col ) );
    } else {
        echo "nothing to process";
    }
?>

你说的“一切都不起作用”是什么意思?返回空字符串或返回带空格的字符串?否,字符串前后相同。您的代码正确使用我!你试过修剪还是修剪?这是一个很大的区别。不@Andreas我不是..我尝试了修剪和ltrim。谢谢你的回复。我试过了,但它不起作用…我正在使用fgets($file1)来读取文件,可能是因为它在每个段落的开头都包含了额外的空白。你确定你不仅仅是想删除
$line=trim(fgets($file1))?当您第一次从文件中读取行时,这将破坏该行周围的所有空间,因此所有其他修剪都是无用的。如果在PHP中对
echo
ed时,在每行的开头都有额外的空格,那么它可能不是来自这段代码。在这段PHP代码之前,您的HTML中可能有额外的空格?在代码读取文本文件(并写入修剪过的内容)之前,有一些东西正在添加额外的空格。。。我们看不到它是什么。这些文件的内容有什么不寻常的地方吗?我只是把它作为一个测试运行,它去掉了前导空格和制表符,没有什么不寻常的。它只有一行字符串。。看到这个屏幕截图是一个
.wav
文件,不是吗?不,它是一个txt文件,保存名称为“audioid_0b098868d0b0ab4337aa9-calldate_20170606.wav”。txt请回复任何一个。。。我还在等吗?
<?php

    $folder_path='c:/wwwroot/content/stack';



    /* find all files in chosen folder */
    $col=glob( $folder_path . '\*.*' );

    /* filter only text files for processing */
    $col=preg_grep( '@(\.txt$)@i', $col );

    /* Proceed if there are text files only */
    if( !empty( $col ) && count( $col ) > 0 ){

        /* iterate through collection */
        foreach( $col as $index => $filepath ){

            /* read file contents into an array */
            $lines=file( $filepath );

            /* store altered lines in temp array */
            $content=array();

            /* trim the start of each line & store into temp array */
            foreach( $lines as $line )if( !empty( $line ) ) $content[]=ltrim( $line );


            /* output file contents */
            file_put_contents( $filepath, implode( PHP_EOL, $content ) );
            echo file_get_contents( $filepath ) . PHP_EOL . PHP_EOL;
        }
        printf( "finito! %d files processed",count( $col ) );
    } else {
        echo "nothing to process";
    }
?>
<?php

    $folder_path='c:/wwwroot/content/stack';



    /* find all files in chosen folder */
    $col=glob( $folder_path . '\*.*' );

    /* filter only text files for processing */
    $col=preg_grep( '@(\.txt$)@i', $col );

    /* Proceed if there are text files only */
    if( !empty( $col ) && count( $col ) > 0 ){

        /* iterate through collection */
        foreach( $col as $index => $filepath ){

            /* read file contents into an array */
            $lines=file( $filepath );

            /* store altered lines in temp array */
            $content=array();

            /* trim the start of each line & store into temp array */
            foreach( $lines as $line )if( !empty( $line ) ) $content[]=ltrim( $line );


            /* output file contents */
            file_put_contents( $filepath, implode( PHP_EOL, $content ) );
            echo file_get_contents( $filepath ) . PHP_EOL . PHP_EOL;
        }
        printf( "finito! %d files processed",count( $col ) );
    } else {
        echo "nothing to process";
    }
?>