FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


styling text string read from plain text file





riv_
OK, so I'm reading data as an array from a plain text file... I want to put an nl2br only where it comes across an empty line... is there a way I can do this?
Also, is there anyway I can get it to treat each bundle of data (before and after the empty line, as a new array - ultimately, I'd like each group to be stuck between <li></li> tags, and then format each field in the array a little differently (first bold, second italic, third plain, etc.)
Should be easy, but this learning curve is pretty steep (for me, anyways) Embarassed
BlackSkad
I guess it is something like this you want. I commented the php, so it should be pretty clear. The format is done with css.
(the \n appended to each line is to make the source-code more readable Wink )
Code:
<html>
<head>
   <title>Test</title>
   <style>
       .alinea1 {text-decoration: underline;}
       .alinea2 {font-weight: bold;}
       .alinea3 {font-style: italic;}
   </style>
</head>
<body>
<ul>
   <li class="alinea1">
<?php

//read the file
$content = file("test.txt");

//count the alinea
$i = 1;
//loop over each line
foreach ($content as $line) {
   //trim the line, so we're sure it can empty (no remaining spaces)
   $line = trim($line);
   //if the line is empty, add </li><br /><li class="alinea$i">
   if (empty($line)) {
      $line .="</li>\n<br />\n<li class=\"alinea".++$i."\">";
   }
   //echo the line
   echo $line."\n";
}

?>
   </li>
</ul>
</body>
</html>
riv_
BlackSkad;
You are my hero Very Happy Shocked Razz Embarassed
Quote:
$line = trim($line);

...that's good
But,
Quote:
if (empty($line)) {
$line .="</li>\n<br />\n<li class=\"alinea".++$i."\">";
}

...that is truly a thing of beauty. Already set up for my CSS and everything.

I don't know if I'll ever get the hang of this. That is a truly wonderful thing and someday, I'll think of it myself. How do you learn all this stuff, anyways?

(thankyouthankyouthankyou)
</gushing>
riv_
OK, so I'm going to be picky after all my gushing.
Looking closer and playing around a bit... this doesn't quite do what I want it to do. I actually had something already that did this...
My plain text file looks like this:
Code:
Item1
Item2
Item3

Item1
Item2
Item3

etc...


I want item1 bold, 2 italic, 3 plain...
The problem with adding the increment when we come across an empty line, is that then the first 3 items are bold, the second 3 italic, and so on.
I need to increment on every line, and then reset to 1 on an empty line.
And I don't know if I can. (No, I can't - I don't know if it an be done)
Does that make sense?
Is it do-able?
Kaneda
riv_ wrote:
The problem with adding the increment when we come across an empty line, is that then the first 3 items are bold, the second 3 italic, and so on. I need to increment on every line, and then reset to 1 on an empty line.


Not sure if this is what you want, but modified BlackSkad's code a bit:

Code:
<html>
<head>
   <title>Test</title>
   <style>
       .alinea1 {text-decoration: underline;}
       .alinea2 {font-weight: bold;}
       .alinea3 {font-style: italic;}
   </style>
</head>
<body>
<ul>
<?php

//read the file
$content = file('test.txt');

//count the alinea
$i = 1;
$group = '';
//loop over each line
foreach ($content as $line) {
   //trim the line, so we're sure it can empty (no remaining spaces)
   $line = trim($line);
   //if the line is empty, add </li><br /><li class="alinea$i">
   if (empty($line)) {
      // Could refactor this into a function:
      $line = '<li>' . $group . '</li>';
      $group = '';
      echo $line."\n";
      $i = 1;
   } else {
      $group .= '<span class="alinea' . $i++ . '">' . $line . '</span> ';
   }
   //echo the line
}

// In case the last group isn't terminated by an empty line:
if (!empty($group)) {
   // Could refactor this into a function
   $line = '<li>' . $group . '</li>';
   $group = '';
   echo $line."\n";
}

?>
</ul>
</body>
</html>


If you need linebreaks at the end of each item in each group, you'd obviously just add '<br>' at the end of the line building the <span>.
riv_
Kaneda;
Thankyou... it's just what I wanted! Razz
It's now broken up into groups and classes that I can style just the way I want them!
Honestly - how do you guys learn this stuff? I feel like every time I learn another thing, I find 20 more things I can't do. I think I must be looking in the wrong places!
Thanks for saving my butt!
riv_
Works like a charm, but I don't quite understand how $group=' ' groups the 3 $lines together? Can you explain that to me?
I'd really like to learn so I can figure it out myself someday, rather than having to be spoon fed all the scripts I need! Embarassed
Kaneda
riv_ wrote:
Works like a charm, but I don't quite understand how $group=' ' groups the 3 $lines together? Can you explain that to me?
I'd really like to learn so I can figure it out myself someday, rather than having to be spoon fed all the scripts I need! Embarassed


It was done quickly, might be a better way, but this was the most readable I could come up with in a couple of minutes Wink (I also notice I forgot to remove some comments, like "//echo the line"). I don't know your level of PHP, so forgive me if I state the obvious and answer the parts you already knew... The $group groups the lines together simply by using the .= operator, which is similar to +=, -= etc.

Code:
$b .= 'blah';


... is shorthand for:

Code:
$b = $b . 'blah';


So, for each line, the <span class="alinea[classno]">[line]</span> stuff is added to $group (which starts out being an empty string) - until the line is empty. Then the accumulated contents of $group are output, $group is reset to the empty string, ready for adding the next lines, and the classno is reset to 1.

Any answer there? Smile

Also might be confusing that I use $line both for the output and the grouping, you could (and should) change the lines in the "if (empty($line))" clause to:

Code:
  $group = '<li>' . $group . '</li>';
  echo $group . "\n";
  $group = '';
  $i = 1;


Makes a bit more sense, so $group is the generated html, while $line is the input.

The use of a loop with an extra test after it (the part to ensure the last group is output even if there was no empty line at the end of the file), could also be done more tightly Smile But it works.
riv_
Thanks, Kaneda.
I didn't even know about the .= operator! Thankyou, it makes perfect sense now.
Related topics
Search function
A ActionScript,showing a running date.
New CMS - ToendaCMS - No database necessary!
How to change text in file field?
Adding field to message data
Excel SpreadSheet to Website
PHP Read File Help needed.
Urgent - Retrieving File Extension From A String.
HELP, reading text file from USB drive in visual basic
PHP File Read, and File Check?
Read file backwards
Which religion / belief system the greates appeal to you?
Need to find text in php files
Python - Using files
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.