hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP Script for Combining Words
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP Script for Combining Words

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-07-2004, 09:17 AM
Corey Bryant Corey Bryant is offline
Texan at Heart
 
Join Date: Jul 2003
Location: Castle Pines, CO
Posts: 7,189

PHP Script for Combining Words


I had a client that is looking for a PHP script that outputs words separately & in combination:

For example lets say i have the following words in a text file..

Cat
Dog
Fish

I would paste them into the script and it would then output something like this..

Cat
Dog
Fish
CatDog
CatFish
DogCar
DogFish
FishCat
FishDog

Of course I know about all the script site but trying to even figure out what to search for, I am at a loss.

Thanks!

Reply With Quote


Sponsored Links
  #2  
Old 12-07-2004, 09:43 AM
cguimont cguimont is offline
Junior Guru Wannabe
 
Join Date: Dec 2004
Posts: 33
Well, this would be a pretty complicated script.
First thing would be to put them into an array.
First, you need a Loop that will pass them 1 by 1.
THen a loop that will pass all the word, and have another loop in the second loop that passes all the words and stick them with the first one.

Reply With Quote
  #3  
Old 12-07-2004, 11:01 AM
gogocode gogocode is offline
Web Hosting Evangelist
 
Join Date: Mar 2004
Location: New Zealand
Posts: 527
Re: PHP Script for Combining Words

PHP Code:
$wordsarray = array('cat''dog''fish');
for(
$x 0$x<count($wordsarray);$x++) { echo $wordsarray[$x]."\n"; for($y 0$y count($wordsarray);$y++){echo $wordsarray[$x] . $wordsarray[$y]."\n";}} 

__________________
James Sleeman
PHP Programmer | Specialist PHP 5 Web Hosting

Reply With Quote
Sponsored Links
  #4  
Old 12-07-2004, 11:11 AM
Corey Bryant Corey Bryant is offline
Texan at Heart
 
Join Date: Jul 2003
Location: Castle Pines, CO
Posts: 7,189
Thanks! I'l keep that in mind. Sonicgroup gave me this code & with a few tweaks - it seems to work:
Code:
// Text file containing words 
$txt = '/path/to/wordfile.txt'; 

$handle = fopen($txt, 'r'); 

if (!$handle) { 
   die('Could not open...'); 
} 

// set up arrays for storing words and concatenated words 
$words = array(); 
$cwords = array(); 

// get the words 
while (!feof($handle)) { 
   // strip_newlines is not a real function - needs to be implemented 
   array_push($words, strip_newlines(fgets($handle))); 
} 

// while we still have words 
for ($i = 0; $i < count($words); $i++) { 
   // get the current word 
   $curword = $words[$i]; 
   // loop over the words 
   foreach ($words as $word) { 
      // if the word is the same as the current word, skip it - i.e. no CatCat 
      if ($word == $curword) { 
         continue; 
      } 
      // append the concatenated word to the array 
      array_push($cwords, $curword.$word); 
    } 
} 

foreach ($words as $var) echo $var."";
foreach ($cwords as $var2) echo $var2."";

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Pingdom Talks Top Web Hosting Cities and Countries Web Hosting News 2013-03-27 18:49:54
Whistleblower Site Cryptome Hacked, Infects PCs with Drive-By Exploits Web Hosting News 2012-02-14 14:48:24
Control Panel cPanel Launches New Apache Configuration Script Web Hosting News 2011-12-28 19:41:39
Web Host HostingZoom Adds Softaculous Auto-Installer to Hosting Plans Web Hosting News 2011-08-17 17:52:34
Web Host JaguarPC Adds Auto-Installer Softaculous to Hosting Plans Web Hosting News 2011-07-27 18:55:46


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?