Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read File extract line by line

Status
Not open for further replies.

ghvn

Technical User
Aug 15, 2001
8
GB
Hi

I'm new to PHP and programming but have managed to make an article creation script. This script indexes and formats user submitted articles. It writes links to articles in a text file, the links are preformatted as list items.

I want to read the first 5 links from this file, strip the formatting and then display them on my main page.

The text file is in this format...

Code:
<li><a href=&quot;Sep-06-213155.php&quot;>Test Article</a><br /><span class=&quot;redtext&quot;><b>Mike</b></span> <span class=&quot;greytext&quot;>6th September 2001</span></li>

I made the following to extract and reformat the first 5 links. It does not work and I am stuck!!


Code:
<?

#open file
	$myfile = &quot;/PATH/TO/data.txt&quot;;

#number of files plus one
	$maxlatest =4;  

	#- Article links file
	if(file_exists($myfile)){
		$fh = fopen($myfile, &quot;r&quot;);
		$old_news = fread($fh, filesize($myfile));
		fclose($fh);
	}

 
	#- get first five article links
	$articles = explode(&quot;</li>&quot;, $old_news);

	$articles = str_replace(&quot;<li>&quot;,&quot;&quot;,$articles);
	$articles = str_replace(&quot;<br />&quot;,&quot; by &quot;,$articles);
	$articles = str_replace(&quot;2001</span>&quot;,&quot;2001</span><br />&quot;,$articles);

	$i=0;
	foreach ($articles as $article){
		if(count($articles)>$i){
			if($maxlatest >= $i++){
				print $article;
			}
		}
	}



?>

Any help would be appreciated, I think I am going along the wrong lines with this :)

Cheers
Mike
 
This will get you started. The following is based on your sample <li>....</li>.

Copy the following and paste it into a new file, upload it and then run it. You will see the following:

Article Link: Sep-06-213155.php
Article Title: Test Article
Article Author: Mike
Article Date: 6th September 2001

Code:
<?php
$string = '<li><a href=&quot;Sep-06-213155.php&quot;>Test Article</a><br /><span class=&quot;redtext&quot;><b>Mike</b></span> <span class=&quot;greytext&quot;>6th September 2001</span></li>';

eregi(&quot;.+href *= *['\&quot;]?([^'\&quot; >]*)['\&quot; >]>*(.+)</a>.+<b>(.+)</b>.+span *.+>(.+)</span>.+&quot;,$string,$data);

echo &quot;<b>Article Link:</b> &quot;.$data[1].&quot;<br>\n&quot;;
echo &quot;<b>Article Title:</b> &quot;.$data[2].&quot;<br>\n&quot;;
echo &quot;<b>Article Author:</b> &quot;.$data[3].&quot;<br>\n&quot;;
echo &quot;<b>Article Date:</b> &quot;.$data[4].&quot;<br>\n&quot;;
?>


Chad. ICQ: 54380631
online.dll
 
Hi Chad, thanks for the help, it is a lot more elegant than my attempt.

I'll let you know how I get on!

Cheers again
Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top