problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

problem

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
mrbeatnik
Forum Newbie
Posts: 4
Joined: Thu Aug 11, 2005 8:47 am

Post 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]++;}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

many thanks got it
Post Reply