Jump to content

Replace text in line $i


daviga404

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);

fclose($fh);

$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;

for($i=1;$i<$linenofinal;$i++){

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
https://forums.phpfreaks.com/topic/250467-replace-text-in-line-i/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.