Jump to content

Replace text in line $i


Recommended Posts

Hi! Basically, I'm making a PHP file that lists things from a file, so if the file contained; '@-Line 1-@@-Line 2-@' it would produce <li><a href="#">Line 1</a></li><li><a href="#">Line 2</a></li>, but I want it so it changes the coding for 1 of every 2 lines. So I've came up with this code:

$myFile = "list.txt";

$fh = fopen($myFile, 'r');

$theData = fread($fh, 100);


$oldtxt = array("@-", "-@");

$newtxt = array('<li><a href="#">','</a></li>');

$newData = str_replace($oldtxt, $newtxt, $theData);

$array = explode("</li>", trim($newData));

$lineno = count($array);

$linenofinal = $lineno - 1;


if(($i % 2)=='0'){




The bit that I'm stuck on is inside the if statement. How could I get it to replace for example <li> in line $i with <li class="1in2">


Thanks in advance,

Daviga404 =D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.