Page 1 of 1

problem

Posted: Thu Aug 11, 2005 5:03 am
by shiznatix
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

Posted: Thu Aug 11, 2005 8:44 am
by Ambush Commander
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.

Posted: Thu Aug 11, 2005 9:08 am
by mrbeatnik
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.

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]++;}

Posted: Thu Aug 11, 2005 9:24 am
by shiznatix
many thanks got it