PDA

View Full Version : PHP Script for Combining Words


Corey Bryant
December 7th, 2004, 14:20
I had a client that is looking for a PHP script that outputs words separately & in combination (actually it is a reseller whose client is hosting on another server but yet I stilll try to provide the server :) ):

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!

sonicgroup
December 7th, 2004, 14:57
I don't know how much programming knowledge you have, but this shouldn't be too hard to write. I doubt there's a premade script out there that will do exactly what you need, as this seems more like a utility function rather than a complete script.

I don't have a lot of time now, so I'll just give you some pseudo code to point you in the right direction. If you need more than that, let me know and I'll implement a working version.

[syntax:8eb1f7fc11="php"]
// 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);
}
}

print_r($words);
print_r($cwords);
[/syntax:8eb1f7fc11]

Corey Bryant
December 7th, 2004, 15:02
Thanks! for PHP - none. My programmers usually handle everything (and they like ASP) so I thought I better not take this to them. LOL. I'll take a look at this & see if it helps them out. It is definitely more than a start!

Corey Bryant
December 7th, 2004, 16:12
We think we got it :) - thanks for the big start!!!
// 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."";

sonicgroup
December 7th, 2004, 17:46
The only thing you need to change is the strip_newlines function. It doesn't exist and would have to be implemented.

Since I have more time now:

[syntax:2ffc47b1ea="php"]
function strip_newlines($str) {
// common newline sequences - \r (carriage return), \n (newline or linefeed), and \r\n (carriage return-linefeed)
$newlines = array(0 => '\r', 1 => '\n', 2 => '\r\n');
// loop over the newline sequences, replacing them with nothing in the string
foreach ($newlines as $nl) {
$str = str_replace($nl, '', $str);
}
// return the cleaned string
return $str;
}
[/syntax:2ffc47b1ea]

Note: This should work, but may not, as I haven't tested it.

Corey Bryant
December 7th, 2004, 22:33
Great - thanks again!!