0

I am reading a textile in php which has two sentences.

Top 1: 201 The Secret

Top 4 : 203, 290, 593, 224

What a Life!, Magical, Unicorn Land, Fire

function getcontent($file1){
    $fh= fopen($file1, 'r');
    $theData = fread($fh, filesize("$file1"));
    return $theData;
    fclose($fh);
}
?>

I wish to highlight the 2 sentences in different colors when I echo the file:

<div align="center"><h4 style="line-height:150%;"><?php echo "<pre>"  .$movies. "</pre>"; ?></h5></div>

$movies is the textfile. How can I do so in php? Would I have to create a separate function for that?

minks
  • 2,859
  • 4
  • 21
  • 29

2 Answers2

1

You could use an array to store the different sentences, so the new code would be something like this. By the way, code after a return statement is never executed, so avoid that

function getcontent($file1)
{
    $lines = file($file); //read all lines to array
    return $lines;
}
<div align="center">
    <h4 style="line-height:150%;">
        <?php
            //lines is the array returned from getcontent
            foreach($lines as $line)
            {
                echo "<pre>"  .$line. "</pre>"
            }
         ?>
    </h5>
</div>

After that you could use an if-clause to swap colors so each row has an alternating color, or a random color if you'd like that

Krysvac
  • 67
  • 8
  • foreach($lines as $line){ echo ' "
    "  .$line. "
    "
    ';} I tried this but I think the html, php and
     are resulting in no output.
    – minks Feb 21 '16 at 09:02
1

PHP's file_get_contents function is a simpler way to get your file's contents.

If your file only ever has 2 lines, there may be a simpler way to do this, but you could split the lines via explode (for details on exploding text that might have strange line breaks, try here), which would look something like this:

$movies = file_get_contents("filename.txt");
$lines = explode("\n", $movies);

Then loop through the lines and style as desired:

if (is_array($lines)) {
    $line_count = count($lines);
    for ($i = 0; $i <= $line_count; $i++) {
        if ($i % 2 == 0) {
            echo '<span style="color: red;">' . $lines[$i] . '</span><br>';
        }
        else {
            echo '<span style="color: blue;">' . $lines[$i] . '</span><br>';
        }
    }
}

More detailed logic could be implemented to color lines differently if there are more than 2 lines in the file.

Per your comment, the following code will color the first line red, and all other lines blue:

if (is_array($lines)) {
    $line_count = count($lines);
    for ($i = 0; $i <= $line_count; $i++) {
        if ($i == 0) {
            echo '<span style="color: red;">' . $lines[$i] . '</span><br>';
        }
        else {
            echo '<span style="color: blue;">' . $lines[$i] . '</span><br>';
        }
    }
}
Community
  • 1
  • 1
Davis
  • 856
  • 4
  • 11
  • Hi, its technically 3 lines actually, I meant Top 1 is one sentence and top 4 labels along with the names are the second sentence(combined). So currently, the code returns the first 2 lines in red and the names in blue. If i want the top 1 in red and top 4 along with the topic in blue, how can I do so? – minks Feb 21 '16 at 10:25
  • See my edit above that addresses your specific need. Thanks. – Davis Feb 22 '16 at 06:51