Warning: All this stuff is untested
i have an array of sentences.
Code: Select all
$sentences = array( 'This is a sentence.', 'This is another sentence.', 'FUBAR means f\'ed up beyond all recognition');
now i send that array through a function and it makes a link, just 1 link in a random spot.
Code: Select all
function arrayRandomLink($sentences,$url) {
//randomly choose a sentence.
$key_sentence = array_rand($sentences);
//explode the sentence
$sentence_array = explode(' ',$sentences[key_sentence]);
$key_word = array_rand($sentence_array);
$word = $sentence_array[$key_word];
$word = "<a href=\"{$url}\">".$word."</a>";
//Insert it back in:
array_splice($sentence_array,$key_word,1,array($word));
$sentence = implode(' ',$sentence_array);
array_splice($sentences,$key_sentences,1,array($sentence));
return $sentence_array;
}
well i need to make 50 links in random spots
Code: Select all
for($i = 0; i < 50; i++) {
$sentences = arrayRandomLink($sentences);
}
but i cant double link like have the same word linked twice.
Code: Select all
//Okay, this algorithm may be a little fuzzy. It'll hang if you try to
//insert too many links. You'll have to have a way to mark sentences as
//already checked, but I'm to lazy to do that right now.
function arrayRandomLink($sentences,$url) {
while (true) {
//randomly choose a sentence.
$key_sentence = array_rand($sentences);
//explode the sentence
$sentence_array = explode(' ',$sentences[key_sentence]);
$key_word = array_rand($sentence_array);
$word = $sentence_array[$key_word];
if (substr($word,0,2) == '<a') {
continue;
}
$word = "<a href=\"{$url}\">".$word."</a>";
//Insert it back in:
array_splice($sentence_array,$key_word,1,array($word));
$sentence = implode(' ',$sentence_array);
array_splice($sentences,$key_sentences,1,array($sentence));
break;
}
return $sentence_array;
}
how can i go about taking out any linked part of a sentence that is linked and then put it back into the sentence in the same place after the sentence was given another link
See above code for first question. If you need to give it another link, I'd suggest strip_tags and then apply the link.