Tải bản đầy đủ - 0 (trang)
Hack 87. Add Links to a Block of Text Automatically

Hack 87. Add Links to a Block of Text Automatically

Tải bản đầy đủ - 0trang

Figure5-23.TheAuto-Linkertext-entryform



Athttp://blog.outer-court.com/yahoo/autolinker.php5,youcan

playwithaworkingversionofAuto-Linkerandseehowitworks

indetail.Onceyou'recomfortablewithit,youcanbuildyour

ownversionwiththecodeinthishack.

TheAuto-LinkermakesuseoftwoYahooAPIserviceswhenit



auto-linksagiventext.First,itfindsthesignificantphrases

withinthetextusingYahoo'sTermExtraction

(http://developer.yahoo.net/content/V1/termExtraction.html).

Second,itusestheYahoo!WebSearch

(http://developer.yahoo.net/web/V1/webSearch.html)tofind

thetopwebpageforthisphrase.Allthat'slefttodoisaddthe

correspondingHTMLlinktoeachoccurrenceofthephrase

withinthetext.



5.12.1.TheCode

ThecodeforAuto-LinksisPHP5,sosavethefollowingcodetoa

filecalledautolinker.php5:
































$text=(isset($_POST['text']))?$_POST['text']:''

$rel=(isset($_POST['rel']))?$_POST['rel']:'';

$engine=(isset($_POST['engine']))?$_POST['engine'

$text=stripslashes($text);







echo'

Auto-Linker

';















if($text=='')

{

?>

ThistoolusestheYahooAPItolinksignificantwor

textyouprovide.





$maxLength=2000;

if(strlen($text)>=$maxLength){



$text=substr($text,0,$maxLength-1).'…';

}




















cols="58"rows="8"><















Relation:





[Default]



Nofollow

















 SearchEngine:





Yahoo



Google






















}



Thiscodemakessurethatifthetextparameterhasnotbeen

submitted,thescriptpresentsa