Coloring the echo output in php - php

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?

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>';
}
}
}

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

Related

<ol> Ordering everything as the number 1 when reading a text file in PHP

I am reading a textfile using PHP and the data is being outputted. The issue I am having is with the formatting of the ol and li tags. The baseball players I am outputting all are numbered as number "1." how do I get the numbers to order chronologically?
HTML/PHP
<?php
$filename ='../teammembers.txt';
$readLines = count(file($filename));
$fp = fopen($filename, 'r');
for($i = 1; $i <= $readLines ; $i++)
{
$line = fgets($fp);
$teammembers = trim($line);
print '<ol><li>Team Member: ' . $teammembers. '</li></ol>';
}
fclose($fp);
?>
You're builing a complete list every time inside your loop. Move the <ol> and </ol> outside the loop and only output the list items:
echo "<ol>";
for($i = 1; $i <= $readLines ; $i++)
{
$line = fgets($fp);
$teammembers = trim($line);
print '<li>Team Member: ' . $teammembers. '</li></ol>';
}
echo "</ol>";
You're creating a new list for every item:
<ol><li>Team Member: Mike</li></ol>
<ol><li>Team Member: Frank</li></ol>
<ol><li>Team Member: Henry</li></ol>
What you want is a single list with just a new element for each item:
<ol>
<li>Team Member: Mike</li>
<li>Team Member: Frank</li>
<li>Team Member: Henry</li>
</ol>
Move the <ol> and </ol> parts outside of your loop:
print '<ol>';
for($i = 1; $i <= $readLines ; $i++)
{
$line = fgets($fp);
$teammembers = trim($line);
print '<li>Team Member: ' . $teammembers. '</li>';
}
print '</ol>';
Put the ol outside.
$filename ='../teammembers.txt';
$readLines = count(file($filename));
$fp = fopen($filename, 'r');
print '<ol>';
for($i = 1; $i <= $readLines ; $i++)
{
$line = fgets($fp);
$teammembers = trim($line);
print '<li>Team Member: ' . $teammembers. '</li>';
}
print '</ol>';
fclose($fp);
As I said in the above comment, calling file() will read all of the file into an array for you, so you already have all of the lines loaded and then in the loop, you read the file again.
So once you've called file (I use the options as you don't need the line feed at the end of the line). It then trims all of the lines and then uses implode() to build up the list...
$readLines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$readLines = array_map('trim', $readLines);
echo '<ol><li>Team Member: '.
implode("</li><li>Team Member: ", $readLines).
'</li></ol>';
So with a file...
Mike
Frank
Henry
outputs...
<ol><li>Team Member: Mike</li><li>Team Member: Frank</li><li>Team Member: Henry</li></ol>

PHP Read Text File With Column Separated

I have a text file generated from our banking software which looks like this:
This is my code to print the text file contents line by line:
<?php
$myFile = "20151231.txt";
$lines = file($myFile);
foreach ($lines as $line_num) {
echo htmlspecialchars($line_num)."<br>";
}
It prints like this:
I just want each line that starts with:
====>
I want everything else deleted.
I tried a lot but failed to print lines with the columns separated as it looks in the text file image.
This is how I want each line to print:
====>0518 Intt on Consumer Loan 401010707 108,149.00
Your assistance regarding this will be highly appreciated.
You can print it as a table:
<?php
$myFile = "20151231.txt";
$lines = file($myFile);
echo '<table>';
foreach ($lines as $line_num) {
if (strpos($line_num, '====>') !== false) {
$str = trim(htmlspecialchars($line_num));
echo '<tr>';
echo '<td>' . getColumnText("/====>\d+/", $str) .'</td>';
echo '<td>' . getColumnText("/\s([a-zA-Z\s]+)/", $str) .'</td>';
$secondCol = getColumnText("/\s([0-9]+)/", $str);
echo '<td>' . $secondCol .'</td>';
$thirdCol = end(explode(" ", $str));
if (trim($secondCol) === $thirdCol) {
echo '<td style="text-align:right">' . str_repeat(" ", 10) .'</td>';
} else {
echo '<td style="text-align:right">' . str_repeat(" ", 10) . $thirdCol .'</td>';
}
echo '</tr>';
}
}
echo '</table>';
function getColumnText($pattern, $str) {
preg_match($pattern, $str, $matches);
return trim(current($matches));
}
yes you can do that with strpos or regularexpression and i am just writing code using strpos
<?php $myFile = "text.txt";
$lines = file($myFile);
echo '<table cellspacing="20">';
$linenum = 1;
foreach ($lines as $line_num) {
echo '<tr>';
// check whether line conatain ====>, if you want to check starting of line then just put 0 instead of false in following condition
if(strpos($line_num,'====>')!==false)
{
$texts= substr($line_num, strpos($line_num,'====>')+5);
$textarr = explode(" ", $texts);
echo '<td>'.$linenum.'</td>';
foreach($textarr as $arr)
{
echo '<td>'.$arr.'</td>';
}
$linenum++;
//print_r($textarr);
//echo htmlspecialchars($line_num)."<br>";
}
}
echo '<table>';

PHP format split number into separate html tags

How would I go and split number 12345 into something like this with PHP:
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
echo preg_replace('((.))', "<span>$1</span>\n", '12345');
Try with:
$input = 12345;
foreach ( str_split($input) as $char ) {
echo '<span>' . $char . '</span>';
}
With str_split()
<?php
$str="12345";
$str = str_split($str);
foreach ($str as $letter){
echo '<span>'.$letter.'</span>'.PHP_EOL;
}
?>
$str = '12345';
$arr = str_split($str);
foreach ($arr as $char) {
echo '<span>' . $char . '</span>';
}
var arr = explode('', "12345");
Then iterate throgh it with foreach and echo (or do whatever you want with) the tags.
If the input string will be large, something like this will be more memory-friendly and possibly faster because it streams instead of breaking it apart first.
$string = "12345";
for ($i = 0, $j = strlen($string); $i < $j; $i++) {
echo "<span>" . $string{$i} . "</span>\n";
}

applying a random variable randomly

I have code generating a random font and applying a new random font to each line of text and would like to add a
$line = str_replace ("a", "#", $line);
But I would like the chance of this being applied to each line to be 10% rather than applied to the string as a whole. How can i do it? Here's my existing code:
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma", "Geneva", "Times New Roman");
shuffle($fonts);
$output = "";
$lines = array_slice(file("users.txt"), -20, 20);
$i = 0;
foreach ( $lines as $line ) {
if($i == count($fonts)) {
shuffle($fonts);
$i = 0;
}
$output .= '<div style="font-size: ' . rand(15, 23) . 'px; font-family:' . $fonts[$i] . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . "</div>\n";
$i++;
}
echo $output;
I'm just starting to get familiar with php, I feel as though I've got a whole new web to play with! :D
If you'd like to have 10% chance of having your 'a' changed with '#' then try inserting this code:
if(rand(0, 9) == 1)
{
$line = str_replace ("a", "#", $line);
}
It generates an Integer from 0 - 9 and if it equals 1 (10% chance, then it applies your formatting.
so, in te foreach($lines as $line) loop, add this
$rand = rand(0,9);
if($rand == 0)
{
$line = str_replace("a","#",$line);
}

apply different styles to different lines of text in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get random value out of an array
I have code applying a random font to a div of text which is taken from the last 20 lines of a .txt file. I would like to apply a different random font to each line... any pointers?
<?php
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma");
shuffle($fonts);
$randomFont = array_shift($fonts);
$output = "";
$lines = array_slice(file("users.txt"), -20, 20);
foreach ( $lines as $line )
{
$output .= '<div style="font-family:' . $randomFont . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . '</div>';
}
echo $output;
?>
Live demo here.
The code:
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma");
shuffle($fonts);
$output = "";
$lines = array();
for($i = 0; $i < 40; $i++) $lines[] = "line $i";
$i = 0;
foreach ( $lines as $line ) {
if($i == count($fonts)) {
shuffle($fonts);
$i = 0;
}
$output .= '<div style="font-family:' . $fonts[$i] . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . "</div>\n";
$i++;
}
echo $output;
Randomize your fonts:
$Random = $fonts[rand(0, count($fonts) - 1)];
Been thinking about it and have a simpler solution.
<?php
$fonts = array ('font 1', 'font 2', 'font 3'); // 20 entries for the full set
shuffle ($fonts);
while ($font = array_pop ($fonts))
{
$output .= '<div style="font-family:' . $font . ';"></div>';
}
?>
This is obviously not an exact solution to the problem you posted above, and it's not intended to be. It's intended to provide an example of an approach for getting random values that are guaranteed to be unique. You should be able to incorporate the idea expressed here into your own code without too much difficulty.

Categories