TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-07-2008, 11:18 AM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default Random Text

How would I make something this where it doesnt repeat the same words, like so:

I took my dog for a walk
into

took dog my for I a walk


not took took my my for for I a a walk walk
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-07-2008, 11:36 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

not very pretty, took about 1min to knock up xD so theres most likely to be mistakes or easier ways:
PHP Code:
$words 'I took my dog for a walk';
$wordArr explode(' '$words);
$cnt count($wordArr);
$randNumStor = array();
//generate an array of random numbers
//make sure that no duplicates exist
for($i 0$i <= $cnt$i++)
{
    
$randNum rand(0$cnt);

    while(
in_array($randNum$randNumStor))
    {
        
$randNum rand(0$cnt);
    }
    
$randNumStor[] = $randNum;
}
//display the words based the random numbers in random array
for($j 0$j <= $cnt$j++)
{
    echo 
$wordArr[$randNumStor[$j]], ' ';

hope i havnt got the wrong end of the proverbial stick.

Edit: i think i may have gotten the wrong idea of you probelm, i just quickly scan read it and saw random text, sorry if i have got the wrong idea but still there are some things in that script that you could use
to delete duplicates from an array use array_unique()

for example:
PHP Code:
$words2 'I I took took my my dog dog for for a a walk walk';

$wordArray2 array_unique(explode(' '$words2));

foreach(
$wordArray2 as $word)
{
    echo 
$word ' ';

EDIT: no no it appears i have confused myself twice haha reality and fiction have now become a mad twist in my brain hahahahahahaha.... more coffee needed!
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Orc (05-07-2008)
Old 05-07-2008, 11:40 AM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
not very pretty, took about 1min to knock up xD so theres most likely to be mistakes or easier ways:
PHP Code:
$words 'I took my dog for a walk';
$wordArr explode(' '$words);
$cnt count($wordArr);
$randNumStor = array();
//generate an array of random numbers
//make sure that no duplicates exist
for($i 0$i <= $cnt$i++)
{
    
$randNum rand(0$cnt);

    while(
in_array($randNum$randNumStor))
    {
        
$randNum rand(0$cnt);
    }
    
$randNumStor[] = $randNum;
}
//display the words based the random numbers in random array
for($j 0$j <= $cnt$j++)
{
    echo 
$wordArr[$randNumStor[$j]], ' ';

hope i havnt got the wrong end of the proverbial stick.

Edit: i think i may have gotten the wrong idea of you probelm, i just quickly scan read it and saw random text, im keeping it up if i am mistaken, if i have got the wrong idea there are some things in that scrupt that you could use
Works, thanks! but how did it work exactly? its just loops it seems with just adding new elements inside variables. :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-07-2008, 11:53 AM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

on the face of it it may sem complex but its actually quite simple (que pseudo(ish)code)

make an array of words
count how many words we have
loop over and make a random number
check the number to see if it already is in the random number array
if not continue, but if it does exist loop until a random number is produced that is unique

then loop over again but use the random number from the random number array as an index for the word array and echo it

make sense?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-07-2008, 11:56 AM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
on the face of it it may sem complex but its actually quite simple (que pseudo(ish)code)

make an array of words
count how many words we have
loop over and make a random number
check the number to see if it already is in the random number array
if not continue, but if it does exist loop until a random number is produced that is unique

then loop over again but use the random number from the random number array as an index for the word array and echo it

make sense?
yeah yeah, just i havent felt well for the past few days, and well i cant think right, and plus my pc is being slow. too much memroy usage i ugess.

but cant you just use array_unqiue for making them unique words aswell?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-07-2008, 11:58 AM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

no because array_unique filters the array and removes duplicates, therefore it wont return anything we can use to check against, in otherwords you would have to loop alot more and have the array filtered and maybe not end up with enough random numbers, if that makes sense?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-07-2008, 12:01 PM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
no because array_unique filters the array and removes duplicates, therefore it wont return anything we can use to check against, in otherwords you would have to loop alot more and have the array filtered and maybe not end up with enough random numbers, if that makes sense?
i guess, really i feel like a donkey right now. plus my head feels like a baloon. :Ss
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-07-2008, 12:03 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

hehe been there mate, grab yourself a coffee or a redbull (as i do) and be as high as a kite (as i am atm) but i guess if your ill sleep may be a better remedy
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Orc (05-07-2008)
Old 05-07-2008, 12:05 PM   #9 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
hehe been there mate, grab yourself a coffee or a redbull (as i do) and be as high as a kite (as i am atm) but i guess if your ill sleep may be a better remedy
ill probably go play some gta 4 mp cause my head is about to explode(' brains ', $orc); from this flu or whatever it is. -_ but I'll go gte a coffee since redbull is like 10 dollars here.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-07-2008, 12:07 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

sketch, your first code snippet has an off-by-one error related to the $cnt variable. It needs to be count()-1 for the loops and calls to rand else you'll get "Undefined offset" notices.

Also, what's the harm in using something like the following instead of the longer, looping code snippet?
PHP Code:
$words 'I took my dog for a walk';
$shuffled explode(' '$words);
shuffle($shuffled);
echo 
implode(' '$shuffled); 
Salathe is offline  
Reply With Quote
Old 05-07-2008, 12:10 PM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
sketch, your first code snippet has an off-by-one error related to the $cnt variable. It needs to be count()-1 for the loops and calls to rand else you'll get "Undefined offset" notices.

Also, what's the harm in using something like the following instead of the longer, looping code snippet?
PHP Code:
$words 'I took my dog for a walk';
$shuffled explode(' '$words);
shuffle($shuffled);
echo 
implode(' '$shuffled); 
Niw, to be honest, thats what I was thinking all along, but I didn't think it work :S
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-07-2008, 12:15 PM   #12 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

yea thanks your correct Salathe i overlooked that, infact i didnt think it through properly (as i previously said) tend to loose track of index offsets for some reason, i guess my brain just brushes it aside as a minor inconvience. Still it begs the question why is an undefined offset a warning and not a fatal in php? im sure most other languages generate a compile error (or is that writing past the end of an array, bah)

and i havnt personally used the shuffle() function before i havnt really had mush need for shuffling an array before so it hasnt come to my attention yet, so there isnt any 'harm' in it in fact its probably quicker (as its a language function therefore written and compiled in C and doesn't need to go through the interpreter)

bah i should actually be working right now, so i guess ill get on with it before i get sacked.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-07-2008, 12:17 PM   #13 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
yea thanks your correct Salathe i overlooked that, infact i didnt think it through properly (as i previously said) tend to loose track of index offsets for some reason, i guess my brain just brushes it aside as a minor inconvience. Still it begs the question why is an undefined offset a warning and not a fatal in php? im sure most other languages generate a compile error (or is that writing past the end of an array, bah)

and i havnt personally used the shuffle() function before i havnt really had mush need for shuffling an array before so it hasnt come to my attention yet, so there isnt any 'harm' in it in fact its probably quicker (as its a language function therefore written and compiled in C and doesn't need to go through the interpreter)

bah i should actually be working right now, so i guess ill get on with it before i get sacked.
Well since the way I was thinknig would work, this thread was for nothing, cause I thought of that first xD. oh well, im going to go rest ( gta 4 mp ) and then go to sleep
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:17 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design