Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/186676-add-text-to-beginning-of-lines/
Share on other sites

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);
?>

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');
    
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.