Накидал вот простенькую функцию, которая запихивает в текст ссылки. Т.е. берется текстовый файл и туда пихаются ссылочки с плотностью, например, 7%. Функция простенькая, писал себе под один проект. Наверно, весь функционал редбаттона по отдельности скоро напишу, надо свой дорген замутить.
Переменные вызова:
$text, сам текст в который пихаются ссылки. string
$prc, int, процент плотности ссылок.
$links, array, массив со ссылками вида http://site.ru
$nobr, bool, 1 или 0. Если, например, текста 500кб а ссылок 3, указывает, обрубать ли текст после последней ссылки(1) или оставить все 500кб с 3 ссылками.
Листинг:
function AddLinksToText($text, $prc, $links, $nobr){
$pos = 0;
$string = "";
$totalLinks = count($links);
$Words = explode(" ", $text);
$totalWords = count($Words);
$needText = $totalLinks * $prc;
if($totalWords < $needText){
$prc = floor($totalWords / $totalLinks); //calculate recommended percentage if haven't enough text
echo @date("r")." not enough text, decrease density to ".$prc."%\r\n";
$needText = $totalLinks * $prc;
} else {
echo @date("r")." working with text, density is ".$prc."%\r\n";
}
foreach($links as $num=>$lnk){
$pos += $prc;
$Words[$pos] = "<a href=\"".$lnk."\">".$Words[$pos]."</a>";
}
if(!isset($nobr)){
$tmp = array_slice($Words, 0, $needText + 3);
} else {
$tmp = $Words;
}
//return string
foreach($tmp as $somevar){
$string .= $somevar." ";
}
return $string;
}
9 responses
Do you want to comment?
Comments RSS and TrackBack Identifier URI ?
Trackbacks