dreamwest Posted December 30, 2009 Share Posted December 30, 2009 I have a list of movies, but i need to add some info to the beginning of each line: Notorious Fox Searchlight Pictures Dir., George Tillman Jr.; Cast, Jamal Woolard, Angela Bassett, Derek Luke, Antonique Smith, Anthony Mackie, Naturi Naughton, Sean Ringgold, Marc Jon Jefferies Paul Blart: Mall Cop Columbia Dir., Steve Carr; Cast, Kevin James, Jayma Mays, Shirley Knight What i need to do is split each block up into lines and add additional info to them Movie: Notorious Studio: Fox Searchlight Pictures Cast: Dir., George Tillman Jr.; Cast, Jamal Woolard, Angela Bassett, Derek Luke, Antonique Smith, Anthony Mackie, Naturi Naughton, Sean Ringgold, Marc Jon Jefferies Movie: Paul Blart: Mall Cop Studio: Columbia Cast: Dir., Steve Carr; Cast, Kevin James, Jayma Mays, Shirley Knight There are always to blank lines in between each paragraph Quote Link to comment https://forums.phpfreaks.com/topic/186676-add-text-to-beginning-of-lines/ Share on other sites More sharing options...
Goldeneye Posted December 30, 2009 Share Posted December 30, 2009 Here is one solution (admittedly not the "best" or most elegant). Depending on the Operating System PHP is running on (*nix or Windows) <?php $movies = array(); $parts = explode("\r\n", $str); foreach($parts as $movie){ $info = explode("\n", $movie); $movies[] = 'Movie: '.$info[0]."\n".'Studio: '.$info[1]."\n".'Cast: '.$info[2]."\n"; } $str = implode("\r\n", $movies); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186676-add-text-to-beginning-of-lines/#findComment-985883 Share on other sites More sharing options...
laffin Posted December 30, 2009 Share Posted December 30, 2009 I took it as if he's working with a file, rather than arrays. <?php $file=<<<EOT Notorious Fox Searchlight Pictures Dir., George Tillman Jr.; Cast, Jamal Woolard, Angela Bassett, Derek Luke, Antonique Smith, Anthony Mackie, Naturi Naughton, Sean Ringgold, Marc Jon Jefferies Paul Blart: Mall Cop Columbia Dir., Steve Carr; Cast, Kevin James, Jayma Mays, Shirley Knight EOT; file_put_contents('sample.lst',$file); $fr=fopen('sample.lst','rt'); $fw=fopen('sample.2.lst','wt'); while($line=fgets($fr)) { if(strlen(trim($line))) { fputs($fw,"Movie: {$line}"); fputs($fw,'Studio: '. fgets($fr)); fputs($fw,'Cast: '. fgets($fr)); } else { fputs($fw,$line); } } fclose($fr); fclose($fw); header('Content-Type: text/plain'); readfile('sample.2.lst'); unlink('sample.lst'); unlink('sample.2.lst'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186676-add-text-to-beginning-of-lines/#findComment-985926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.