Jump to content

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/243280-wiki-bbcode/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/243280-wiki-bbcode/#findComment-1249409
Share on other sites

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>';
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/243280-wiki-bbcode/#findComment-1249430
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.