Hello friends i am reading a folder in which i have many text files in PHP and i need to remove all the spaces from the starting of each paragraph in text file. But when i am reading file and tried to remove space its not working like i tried TRIM function LTRIM and also tried REGEX "/\s+$/" but all are not working. But when i am using REGEX this "/\s+/" instead of "/\s+$/" its removing all the spaces. I need only remove space from the beginning of paragraph any help please...Below is my code that i am trying..
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);
}
Use /^\s+/
to match spaces at the start of the paragraph. E.g.
$str = preg_replace("/^\s+/", "", $line);
I used something like this a couple of years ago for a similar task - seemed to work ok, perhaps it might be of use - though if you do not wish to edit the file rather than use file_put_contents
simply echo implode(PHP_EOL, $content );
<?php
/* 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 ) ){
/* 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 );
/* write content back to file */
file_put_contents( $filepath, implode( PHP_EOL, $data ) );
}
echo "finito!";
}
?>
The test script used a single text file called lorem.txt
with the following contents.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque suscipit lacus massa, in malesuada quam consectetur at. Proin fermentum augue vel purus pellentesque placerat. Nunc tincidunt mi ac est varius, id euismod est auctor. Mauris facilisis luctus justo tristique consectetur. Vestibulum tempus lacinia blandit. Praesent dui metus, aliquet ut velit vitae, suscipit volutpat tortor. Nunc dictum nunc sed nisi bibendum dictum id vitae lorem. Donec viverra eget odio in hendrerit. Mauris iaculis at eros at scelerisque. Suspendisse a consequat erat, in fringilla diam. Fusce vitae hendrerit lacus. Ut sed purus vel metus semper sagittis. Ut fermentum magna quis tempus scelerisque.
Phasellus quis rhoncus sapien. Nulla bibendum sagittis metus. Praesent elementum ex augue, a ullamcorper velit consectetur in. Maecenas mollis neque eu massa feugiat rutrum. Sed eu aliquam nulla. Sed sit amet egestas eros. Donec commodo leo in pellentesque efficitur. Cras ornare dui iaculis ultricies lobortis. Vivamus vel tellus maximus, sodales odio quis, ultrices metus.
Nullam vitae rhoncus justo, a congue turpis. Nulla facilisi. Nulla faucibus massa et arcu molestie dignissim. Maecenas aliquet posuere eros sit amet vehicula. Praesent quis arcu eros. Quisque vitae dictum orci. Integer quis mollis felis.
Morbi vitae risus fringilla, ornare ipsum ac, tincidunt metus. Ut ac eros placerat, auctor metus non, sollicitudin metus. Aenean malesuada gravida dolor, sit amet blandit lacus dignissim at. In egestas nibh felis, pulvinar laoreet eros rhoncus quis. Donec pulvinar condimentum ex, quis faucibus massa placerat sed. Fusce in mattis sem, sed rutrum nibh. Donec vitae risus urna. Suspendisse potenti. Maecenas vitae neque tempus, malesuada dolor et, rhoncus orci.
Duis quis augue vitae sapien cursus suscipit. Nulla rutrum eget diam in feugiat. Vestibulum posuere dapibus nisl at laoreet. Donec ante arcu, euismod et finibus ut, malesuada sed augue. Nunc porttitor leo ex, eget luctus augue sollicitudin non. Proin nec cursus ipsum. Nulla mi enim, finibus a felis vitae, pharetra tempor tortor. Proin a arcu lectus.
Most of the above lines have either spaces or tabs at the beginning
<?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";
}
?>
Simply by changing the variable $trim
from rtrim
to ltrim
the output is as expected
The following modifies the original file ( don't know if that is an issue ) and was run from the cmdline-all spaces from beginning of lines are removed.
<?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";
}
?>