Page 1 of 1
Printing textually based line numbers
Posted: Thu Apr 05, 2007 12:18 pm
by Seiryu
Okay so I've tried looking for a function that could do this, and quite frankly got lost in the massive string check function libraries. @_@
What I'm trying to accomplish is checking the textual output from a PHP/HTML file, and adding a line break every 100 lines. I understand I can use a loop for that, but what I don't get is how to declare the line numbers as a variable, and how to replace the unaltered output with the altered output so I'm not printing the same page twice.
I heard there was a lineNum() function, but for the life of me I can't find any documentation whatsoever for it. Any help would be much appreciated.

Posted: Thu Apr 05, 2007 12:25 pm
by shiznatix
If I get what you are saying correctly, you have a file that you want to open up, then insert a line break (\n) every 100 lines, then write the changes to the file, correct?
If so, heres how!
Code: Select all
$file = file_get_contents('the_file.txt'); //can be php, html, whatever
$lines = explode("\n", $file);
$new = array();
foreach ($lines as $key => $line)
{
if (100 % $key)
{
$new[] = "\n";
}
$new[] = $line;
}
$input = file_put_contents('the_file.txt', implode("\n", $new));
untested of course.
Posted: Thu Apr 05, 2007 12:53 pm
by RobertGonzalez
HTML line break (<br />) or textual line break (\n\r)? Either way, you could do something like this...
Code: Select all
<?php
// A la shizzy boy
$file = file_get_contents('the_file.txt'); //can be php, html, whatever
$lines = explode("\n", $file);
$break = '<br />'; // or \n
$limit = count($lines);
for ($i = 0; $i < $limit; $i++)
{
if (100 % ($i + 1))
{
$lines[$i] = $lines[$i] . $break;
}
}
$input = file_put_contents('the_file.txt', implode("\n", $lines));
?>
This is totally untested.
Posted: Thu Apr 05, 2007 1:26 pm
by stereofrog
Code: Select all
$text = preg_replace(
'/(.*\n){100}/',
"$0<br>\n",
file_get_contents("input")
);
This adds a html break <BR> every 100 lines.
Posted: Thu Apr 05, 2007 2:49 pm
by Seiryu
While those are helpful, the problem I'm facing is more of trying to seperate the actual content of the page from the source coding. I'm try to generate an HTML/PHP page that modifies itself on load so that it numbers the lines. I've gotten as far as having numbered lines. Is there a function that calls the text from the PHP file itself, without the implemented code?
Posted: Thu Apr 05, 2007 3:09 pm
by RobertGonzalez
So you are trying to show the source of the PHP file with line numbers?
Posted: Thu Apr 05, 2007 3:52 pm
by Seiryu
I'm trying to show the content of the page without the source with line numbers.
Posted: Thu Apr 05, 2007 3:55 pm
by RobertGonzalez
Do you have an example you could link to so we can what you are wanting to do? It almost sounds like you want to add a line number for every piece of content text that outputs to the screen.
Posted: Thu Apr 05, 2007 4:04 pm
by Seiryu
I want to turn this:
<code>Hey! this is<code> some random text
</code>to exemplify</code>
what <code> I'm </code> trying to <code>
achieve.
Into this:
Line 1. Hey! this is some random text
Line 2. to exemplify
Line 3. what I'm trying to
Line 4. achieve.
I've tried doing it myself but I'm honestly just not sure how to tackle this one, and haven't been able to dredge up anything useful in searches.
Eventually I will need to pick out every hundredth line, but I can do that in a for loop easily enough. Thanks for all the help, by the way!

Posted: Thu Apr 05, 2007 4:12 pm
by RobertGonzalez
Now I am seeing what you want. That is going to require the assistance of regexp I believe.
Posted: Thu Apr 05, 2007 6:41 pm
by Seiryu
You can use RegExp in PHP? o.O
How would you go about referencing the function? Or is it just use on demand? Gonna go look this up, that certainly explains a lot!
Posted: Thu Apr 05, 2007 7:21 pm
by RobertGonzalez
PHP has some fairly broad regular expression handling. Check the manual. There is a lot you can do with it.