[RESOLVED] foreach problem - Parsing a webpage

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
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

[RESOLVED] foreach problem - Parsing a webpage

Post 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
Last edited by sikelsh on Thu Mar 23, 2006 5:32 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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 />";
		
	}
	
}
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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?
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

Post by sikelsh »

I do yeh :) Barnsley but work in Wetherby
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Im in York...not too far away from you all....im from Leeds originally tho ;)
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

Post by sikelsh »

well if you ever take up fishing you now know where to visit :)
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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 />";
	
}
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

Post 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]
Post Reply