Make automatic links in text from file: like Wikipedia internal - PHP

I need help with this script I am doing ...

I want my site to be a wikipedia in its own right ... take for example I have a php website ... I post daily articles on it.

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

now i save text / XML text / text and link

Example

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

      

or any methods needed to

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

      

now I want the PHP script to automatically convert any jenna bush or any Michael Jackson linked to their respective links all over my site ...

Any help is greatly appreciated ...

+2


source to share


4 answers


I configured it and here's 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 link creation function just makes the string "abcd is kk" as "abcd-is-kk" to end the tag page;)

greetings!

+1


source


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

Just before sending text to the database, you run a function on it that searches for certain phrases and replaces them with other phrases.

Alternatively and probably the best approach is the same as mediawiki (the software that wikipedia uses to use), every time you want to link to another article in the mediawiki that you put around [[]], for example [ [Michael Jackson]]. This way, you have more control over what becomes the link.



Example: If you had an article about a prince, a musician and one on Prince Charles, and you wanted to contact Prince Charles, the first way would be to find the prince first and link to him, however if you are using the mediawiki method you should write [[ Prince Charles]], and he would know what to look for. For this I recommend preg_match http://www.php.net/manual/en/function.preg-match.php

It might be worth taking a look at how MediaWiki does the same, you can download it for free and it is written in php

+1


source


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

      

0


source


function tags_autolink() 
{
    $conn = mysqli_connect("localhost", "root", "", "sample")
or die ("Could not connect to mysql because ".mysqli_error());

$text = 'You paragraph or text here';
$query_tags_autolink = "SELECT tag from tags";
$rs_tags_autolink = mysqli_query($conn,$query_tags_autolink) or print "error getting tags";

while($row_tags_autolink = mysqli_fetch_array($rs_tags_autolink))
{
$tag_name = trim($row_tags_autolink['tag']);
$trimedurl = str_replace(' ', '-',$tag_name);
$trimedurl=strtolower("$trimedurl");
$tag_url = "http://yourdomain/tag/$trimedurl";
$text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b($tag_name)\b(?!:)(?![^<>]*?>)|imsU","<a href=\"$tag_url\">$1</a>" , $text);
}

return  trim($text);
}

echo tags_autolink() ;

      

0


source







All Articles