| | | Programming Discuss all your programming needs here. |  | 
04-13-2009
| | Banned | | | Join Date: Nov 2007 Location: Germany
Posts: 703
Reputation: 52 Level up: 98%, 18 Points needed | | | | [Question/Request] Read out Datas from a Website with php? Like the title already says(is that right?): I need help with my script. I want to filter out a little thing form a Website. Its a little Box and theres a Motd wich changes daily. Now I want my website to read out the motd and 'simply' show it in that box of my site.
Here's the code I already made: PHP Code: <?php $url = "http://www.schuelervz.net/";
$startstring = '<div id="LeftsideBox"> <div> <p>';
$endstring = "</p>";
$file = @fopen ($url,"r");
if (trim($file) == "") { echo "Service out of order"; } else { $i=0; while (!feof($file)) {
$zeile[$i] = fgets($file,20000); $i++; } fclose($file); }
for ($j=0;$j<$i;$j++) { if ($resa = strstr($zeile[$j],$startstring)) { $resb = str_replace($startstring, "", $resa); $endstueck = strstr($resb, $endstring); $result .= str_replace($endstueck,"",$resb); $result .= "; "; } }
$result = ".$result"; return $result; ?> | Donate to remove ads, get your "DONATOR title, and get access to the MMOwned community's elite Shoutbawx. 
04-14-2009
|  | Knight-Lieutenant | | | Join Date: Oct 2007 Location: Denmark
Posts: 260
Reputation: 25 Level up: 11%, 626 Points needed |     | | | | 
04-15-2009
| | Banned | | | Join Date: Nov 2007 Location: Germany
Posts: 703
Reputation: 52 Level up: 98%, 18 Points needed | | | Quote:
Originally Posted by Sirupsen | Hmm, maybe you're right, but I just want to read the website in [file_get_contents() or fopen()] and then delete code I don't need and just show that part of the string wich goes from Code: <div id="LeftsideBox">
to Or I just got an other Idea
Is it possible to write something with a php-script and also delete the whole thing (or just blend out) what was written before? | 
04-15-2009
|  | Knight-Lieutenant | | | Join Date: Oct 2007 Location: Denmark
Posts: 260
Reputation: 25 Level up: 11%, 626 Points needed |     | | | Like, theres something by default, when the PHP script runs, the default stuff get's replaced with something else? | 
04-16-2009
| | New User | | | Join Date: Nov 2008
Posts: 14
Reputation: 1 Level up: 61%, 156 Points needed | | | easy trick it's a very easy task: PHP Code: <?php
$url = "http://www.schuelervz.net";
$version = phpversion();
function a_strstr($haystack, $needle, $before_needle = false) {
if(($pos = strpos($haystack, $needle)) === false) { return false; }
if($before_needle) { return substr($haystack, 0, $pos + strlen($needle)); }
return substr($haystack,$pos);
}
$pat1 = '<div id="LeftsideBox">
<div>
<p>';
$pat2 = '<p>';
$pat3 = '</p> </div>
</div>
</div>
<br class="Clear-The-Evil-Float" />';
$pat4 = '</p>';
$content = file_get_contents($url);
$content = a_strstr($content, $pat1, false);
$content = a_strstr($content, $pat2, false);
$content = substr($content, 3);
$content = a_strstr($content, $pat3, true);
$content = a_strstr($content, $pat4, true);
$content = substr($content, 0, strlen($content)-4);
echo $content;
?> If you have PHP 5.3.0 installed you can delete the function declaration and have to rename a_strstr to strstr. Because PHP 5.3.0 brings the befor/after flag for this function and so my additional function is noch needed. | 
04-16-2009
|  | Contributor | | | Join Date: Jan 2008
Posts: 277
Reputation: 116 Level up: 27%, 658 Points needed |     | | | The easiest way to do this is with rss and a php rss parser. You barely have to write anything.
If you site already pushes rss then you just need to get a php rss script and throw in some tags.
__________________ _____________ _ _
|--|--|==|--|-:_Y | 
04-16-2009
| | New User | | | Join Date: Nov 2008
Posts: 14
Reputation: 1 Level up: 61%, 156 Points needed | | | | The Problem here is that the specific site doesn't have a RSS feed for there motd, so sheepking want to leech that info.
XML Parsing is a really easy task in php and other language too, but that was not the question | 
04-23-2009
| | Site Donator | | | Join Date: Oct 2008 Location: A fridge somewhere
Posts: 17
Reputation: 6 Level up: 84%, 65 Points needed | | | | I sent you a pm. I have done these types of things before. So it shouldn't be so hard to do it again. Just need the site url. What part you need extracted and i will cook you up some code. | 
04-26-2009
| | Site Donator | | | Join Date: Oct 2008 Location: A fridge somewhere
Posts: 17
Reputation: 6 Level up: 84%, 65 Points needed | | | | Solution. Here is the working code. I stuck it in a function so you can call it whenever you want to.
Have fun Code: function grabHTML() {
//Set this value to true to strip out only the <div> tags
$stripDivs = false;
//Set this value to true to strip out all html tags
$stripHtml = false;
//The 2 values below are used for the script.
$html = "";
$boolLeftSideBox = false;
//Open the site and start reading.
$website = file('http://www.schuelervz.net/Default');
foreach($website as $line) {
if(eregi('<div id="LeftsideBox">', $line)) {
$boolLeftSideBox = true;
continue;
}
if($boolLeftSideBox) {
if(eregi('<div>', $line)) {
$html .= $line;
} else {
$html .= $line;
if($stripDivs) {
$html = str_replace('<div>', '', $html);
$html = str_replace('</div>', '', $html);
}
if($stripHtml) {
$html = strip_tags($html);
}
return $html;
}
}
}
}
echo grabHTML();
| 
04-27-2009
| | Banned | | | Join Date: Nov 2007 Location: Germany
Posts: 703
Reputation: 52 Level up: 98%, 18 Points needed | | | | Very, very good!
Works!
Thank you very much!
+rep x2
(Just the prob that I'm using a Free hoster wich is forbidding the most of your arguments, but works fine with xampp) |  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -4. The time now is 02:41 AM. |