doddsey_65 Posted July 30, 2011 Share Posted July 30, 2011 Im trying to set up a unique bb code where the user enters [wiki]george clooney[/wiki]. What i want to do is return the infobox that wikipedia has on the right of its pages. This box is held in a table with class "infobox" so i have been trying to return it, but to no avail. I have tried using $content = stream_get_contents($handle); which returns all of the html for the wiki page, but i dont know how to extract the info box from this. Anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/243280-wiki-bbcode/ Share on other sites More sharing options...
arbitter Posted July 30, 2011 Share Posted July 30, 2011 Well by the looks of it you need the table with the class='infobox *' There's something after the infobox though; sometimes 'biography vcard' (with george clooney), but it's 'vevent' in an event, so probably there's dozens of these classes, but that's the part you need to split it at. Quote Link to comment https://forums.phpfreaks.com/topic/243280-wiki-bbcode/#findComment-1249409 Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 thanks for the reply. I have managed to do it after some hard work and alot of problems. Firstly the wiki bbcode looks like [wiki]jenson button[/wiki] the function then opens the url http://en.wikipedia.org/wiki/Jenson_button as you can see spaces are automatically replaced with underscores and the first letter is automatically capitalized. The problem is that this page is null because the person who created the page also capitalized "button". Also there was the problem with pages that have no images in the info box. This only became a problem since i wasnt retrieving the full info box but just as far as the image. Well heres the code i used to achieve it. Ive ran a few tests and they all work. The only problem is that it is very slow so if anyone has any ideas on speeding it up then please let me know. function wiki_preview($page) { $info_box = ''; $link = 'http://www.en.wikipedia.org/wiki/'.$page; // if there is no page at the given address then capitalize all // letters. if(!$lines = @file($link, FILE_IGNORE_NEW_LINES)) { $page = ucwords($page); } $link = 'http://www.en.wikipedia.org/wiki/'.str_replace(' ','_',$page); // open the file as an array $lines = @file($link, FILE_IGNORE_NEW_LINES); // if we could open the file if($lines) { foreach($lines as $line_num => $line) { // if this page has an info box if(preg_match('|\<table class=\"infobox|si', $line, $matches)) { // get the lines from the start of the infobox for($i=$line_num; $i<100; $i++) { // if this isnt the first closing td tag then we continue if(!preg_match('|\<\/td\>|si', $lines[$i-1])) { // need to replace the relative image link with an absolute one $info_box .= preg_replace('|\/wiki\/File\:|si', 'http://en.wikipedia.org/wiki/File:', $lines[$i]); } // else we return what we have else { return '<a target="_blank" href="'.$link.'">'.$info_box.'</td></tr></table></a>'; } } } } } // if it couldnt find the page else { return '<div class="infobox">This entry does not exist on wikipedia</div>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/243280-wiki-bbcode/#findComment-1249430 Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 okay i have a problem when uploading the script to my live server. Wikipedia throws back an access denied error. Is there any way to get around this? Quote Link to comment https://forums.phpfreaks.com/topic/243280-wiki-bbcode/#findComment-1249466 Share on other sites More sharing options...
arbitter Posted July 30, 2011 Share Posted July 30, 2011 Well I'm not really into these things, but I presume you have to use the wikipedia API to access the data. Quote Link to comment https://forums.phpfreaks.com/topic/243280-wiki-bbcode/#findComment-1249503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.