Printing textually based line numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Seiryu
Forum Newbie
Posts: 5
Joined: Thu Apr 05, 2007 12:07 pm

Printing textually based line numbers

Post 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. :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
Seiryu
Forum Newbie
Posts: 5
Joined: Thu Apr 05, 2007 12:07 pm

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you are trying to show the source of the PHP file with line numbers?
Seiryu
Forum Newbie
Posts: 5
Joined: Thu Apr 05, 2007 12:07 pm

Post by Seiryu »

I'm trying to show the content of the page without the source with line numbers.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Seiryu
Forum Newbie
Posts: 5
Joined: Thu Apr 05, 2007 12:07 pm

Post 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! :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Now I am seeing what you want. That is going to require the assistance of regexp I believe.
Seiryu
Forum Newbie
Posts: 5
Joined: Thu Apr 05, 2007 12:07 pm

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

PHP has some fairly broad regular expression handling. Check the manual. There is a lot you can do with it.
Post Reply