views:

545

answers:

5

Hello,

I want help on this script I am making...

I want my website to be a wikipedia in itself... take for example I have a php website... I publish daily articles on it.

Suppose I publish 2 articles on Jenna Bush and Michael Jackson respectively

now I save into text/xml/database text and link

example

jenna bush, http://www.domain.com/jenna.html    
michael jackson, http://www.domain.com/michael.html

or any which ways required like

<xml>
<item>
<text>jenna bush</text>
<link>http://www.domain.com/jenna.html&lt;/link&gt;
</item>
... etc
</xml>

now what I want is the PHP script should automatically convert any jenna bush or any michael jackson linked to their respective links all over my website...

Any help is much appreciated...

+1  A: 

Assuming that the text containing those words are in the database the best way to achieve something like that is using str_replace http://ie2.php.net/manual/en/function.str-replace.php

Right before the text is submitted to the database you run a function on it that looks for certain phrases and replaces them with other phrases.

Alternatively and probably a better approach is the same one that mediawiki (the software that wikipedia runs on uses), everytime you want to create a link to another article in a mediawiki you put [[ ]] around it, for example [[Michael Jackson]]. That way you have more control over what becomes a link.

Example: If you had an article on Prince the musician and one on Prince Charles and you wanted to link to Prince Charles, the first method might find Prince first and link to him, however if you use the mediawiki method you would write [[Prince Charles]] and it would know what to look for. To do that I'd recommend preg_match http://www.php.net/manual/en/function.preg-match.php

It may be worth having a look at how mediawiki does the same thing, you can download it for free and it's written in php

Stephen lacy
Thanks Stephen but that doesn't serve my purpose here ... what about the already published 3000 articles... and moreover... what about the words coming in the future... what I need is something like a function that will take an array of words probably from a text or xml file... match and replace it to linked text in the text using preg match dynamically on a page load... so when ever there is a new entry at the backend... the front end word changes to a link... ;)
Pushpinder
then go with my first suggestion and use str_replace, or perhaps str_ireplace would be better, just make sure when you go through your list of phrases to make into links you order by phrase string length descending to avoid the problem I mentioned
Stephen lacy
hmm okay thanks... will try and also search for more alternatives... will update here... :D
Pushpinder
A: 

what I was searching for was this

http://seoroi.com/specialty-services/new-seo-plugin-for-wordpress-internal-link-building/

and

http://www.centrostudilaruna.it/huginnemuninn/2007/12/06/wordpress-plugin-tags-autolink/

these are made for wordpress... all I need is to understand these and convert them to my custom needs...

any clues ?

Pushpinder
A: 

I customized it and here is for everyone interested

function tags_autolink($text) 
{

$text = " $text ";
$query_tags_autolink = "SELECT tag from tags";
$rs_tags_autolink = mysql_query($query_tags_autolink) or print "error getting tags";

while($row_tags_autolink = mysql_fetch_array($rs_tags_autolink))
{
$tag_name = trim($row_tags_autolink['tag']);
$tag_url = "http://www.domain.com/tag/".createLink(trim(htmlentities($tag_name)))."/";
$text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b($tag_name)\b(?!:)(?![^<>]*?>)|imsU","<a href=\"$tag_url\">$1</a>" , $text);
}

return trim( $text );
}

the create link function simply makes a string of "abcd is kk" like "abcd-is-kk" for a tag page ending ;)

cheers !

Pushpinder
A: 

its not working perfectly with me its giving no error (after i change names (inc. Table, Database etc)

but not working too

Simply giving no Result, not auto linking text from Database links

Meer
its working for me, probably if you could paste your code here I could see what's wrong. I do not know the wordpress thing but the php installation
Pushpinder
A: 
function auto_href($x)
        {
        $x = explode(' ', $x);
        foreach ($x as $y)
                {
                if (substr($y, 0, 7) == 'http://')
                        $y = '<a href="'.$y.'">'.$y.'</a>';
                $z[] = $y;
                }
        return implode($z, ' ');
        }