ok so here is the problem.
i have an array of sentences. now i send that array through a function and it makes a link, just 1 link in a random spot. well i need to make 50 links in random spots but i cant double link like have the same word linked twice. 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
problem
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Warning: All this stuff is untested
i have an array of sentences.
now i send that array through a function and it makes a link, just 1 link in a random spot.
well i need to make 50 links in random spots
but i cant double link like have the same word linked twice.
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');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;
}Code: Select all
for($i = 0; i < 50; i++) {
$sentences = arrayRandomLink($sentences);
}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;
}See above code for first question. If you need to give it another link, I'd suggest strip_tags and then apply the link.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
You could have a second array, marking the amount of times used.
array1(msg1, msg2, msg3)
array2(0,0,0)
Depending on the position, you can check the amount of times used.
array1(msg1, msg2, msg3)
array2(0,0,0)
Depending on the position, you can check the amount of times used.
Code: Select all
//get random word finder number
//some code to give:
$i=3;
if(array2[$i]==0){
//~pull out word (msg3 for example).
$msg=array1[$i];
//~increase the count on the corresponding array.
array2[$i]++;}