Page 1 of 1

[RESOLVED] foreach problem - Parsing a webpage

Posted: Thu Mar 23, 2006 4:23 am
by sikelsh
Sorry for the title, not sure what to call this

using various things ive read and picked up today, ive managed to get this far (not that bad for newb I guess), however I cant seem to get it work properly

What im trying to do, is parse the most recent posts on Proboards Forum, using PHP, I want the link, the title and comments, this eventually will form part of a RSS feed (programming still do, not my current problem)

I cant seem to get this to keep parsing, it only does the first entry

heres the link im parsing

http://yorksfishing.proboards4.com/inde ... ion=recent

heres my code so far

Code: Select all

<?php
$handle = fopen("http://yorksfishing.proboards4.com/index.cgi?action=recent", "rb");

if ($handle)
{
 while (!feof($handle)) {
   $contents .= fread($handle, 8192);
 }

if ($contents)
        {
	$start= strpos($contents, "Topic:");
	$finish= strpos($contents, "(Read");
	$length= $finish-$start;
	$code=Substr($contents, $start, $length);
	$oldlink  = "/index.cgi";
	$newlink  = "http://yorksfishing.proboards4.com/index.cgi";
	$forumlink = str_replace($oldlink, $newlink, $code);


	$start2= strpos($contents, "<!-- google_ad_section_start -->");
	$finish2= strpos($contents, "<!-- google_ad_section_end -->");
	$length2= $finish2-$start2;
	$code2=Substr($contents, $start2, $length2);
	$oldmarker  = "<!-- google_ad_section_start -->";
	$newmarker  = "Comments: ";
	$forumpost = str_replace($oldmarker, $newmarker, $code2);
	}


echo $forumlink;
echo "<br>";
echo strip_tags($forumpost);
fclose($handle);
}
?>
heres my current output

http://www.yorkshirefishing.net/testrecent.php

I just want the above code, to do what its doing on the first recent post, and carry on for the next 24

Jeese hope that makes some sense

Thanks in advance
Si

Posted: Thu Mar 23, 2006 4:48 am
by JayBird
try this to get you started

Code: Select all

$handle = fopen("http://yorksfishing.proboards4.com/index.cgi?action=recent", "rb");

if ($handle) {
 while (!feof($handle)) {
   $contents .= fread($handle, 8192);
 }
}

preg_match_all('/Topic\:(.*?)\(Read/si', $contents, $topics);

preg_match_all('/<!-- google_ad_section_start -->(.*?)<\!-- google_ad_section_end -->/si', $contents, $comments);

$oldlink  = "/index.cgi";
$newlink  = "http://yorksfishing.proboards4.com/index.cgi";

foreach ($topics as $temp) {
	foreach($temp as $k=>$v) {
	

    	$v = str_replace($oldlink, $newlink, $v); 

		echo $v;
		echo "<br />";
		echo "Comments: ".strip_tags($comments[0][$k]);
		echo "<br /><br />";
		
	}
	
}

Posted: Thu Mar 23, 2006 4:54 am
by sikelsh
Dude I love you :)

Ive got some kinda repitition going off, but thats a massive leap :)

http://www.yorkshirefishing.net/testrecent2.php

Thanks so much

Posted: Thu Mar 23, 2006 4:56 am
by JayBird
No probs buddy...have a play with the code i gave you...it was by no means perfect, but it's a good starting point.

If you then come across any specific question, just ask here.

BTW...do you live in Yorkshire?

Posted: Thu Mar 23, 2006 4:59 am
by sikelsh
I do yeh :) Barnsley but work in Wetherby

Posted: Thu Mar 23, 2006 5:03 am
by jayshields
Cool, I'm in Leeds. I've just been reading another thread with someone else from Leeds too, rm something he was called I think.

Small world.

Posted: Thu Mar 23, 2006 5:04 am
by JayBird
Im in York...not too far away from you all....im from Leeds originally tho ;)

Posted: Thu Mar 23, 2006 5:08 am
by sikelsh
well if you ever take up fishing you now know where to visit :)

Posted: Thu Mar 23, 2006 5:16 am
by sikelsh
I think that once the code had read the page, its starting again.

I need to break it somehow, the closest constant I can find in the source code of the recent posts is Forum Information

I need to tell the code to stop when it see's this text? sorry any clues?

Simon

Posted: Thu Mar 23, 2006 5:21 am
by JayBird

Code: Select all

preg_match_all('/Topic\:(.*?)\(Read/si', $contents, $topics);
Is returning the matches twice for some reason


this should cut down the repetition

Code: Select all

$handle = fopen("http://yorksfishing.proboards4.com/index.cgi?action=recent", "rb");

if ($handle) {
 while (!feof($handle)) {
   $contents .= fread($handle, 8192);
 }
}

preg_match_all('/Topic\:(.*?)\(Read/si', $contents, $topics);

preg_match_all('/<!-- google_ad_section_start -->(.*?)<\!-- google_ad_section_end -->/si', $contents, $comments);

$oldlink  = "/index.cgi";
$newlink  = "http://yorksfishing.proboards4.com/index.cgi";

foreach ($topics[0] as $k=>$v) {

    	$v = str_replace($oldlink, $newlink, $v); 

		echo $v;
		echo "<br />";
		echo "Comments: ".strip_tags($comments[0][$k]);
		echo "<br /><br />";
	
}

Posted: Thu Mar 23, 2006 5:31 am
by sikelsh
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$handle = fopen("http://yorksfishing.proboards4.com/index.cgi?action=recent", "rb");

if ($handle) {
 while (!feof($handle)) {
   $contents .= fread($handle, 8192);
 }
}

preg_match_all('/Topic\:(.*?)\(Read/si', $contents, $topics);

preg_match_all('/<!-- google_ad_section_start -->(.*?)<\!-- google_ad_section_end -->/si', $contents, $comments);

$oldread  = "(Read";
$newread  = "";
$oldlink  = "/index.cgi";
$newlink  = "http://yorksfishing.proboards4.com/index.cgi";

foreach ($topics[0] as $k=>$v) {

        $v = str_replace($oldlink, $newlink, $v);
        $v = str_replace($oldread, $newread, $v);

        echo $v;
        echo "<br />";
        echo "Comments: ".strip_tags($comments[0][$k]);
        echo "<br /><br />";

}
Final code above :) job done

I just added the $newread and $oldread to remove that bit


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]