View Single Post
Old 12-14-2012, 09:34 AM  
Webmaster Advertising
So Fucking Banned
 
Join Date: Sep 2003
Posts: 1,360
Quote:
Originally Posted by cemtex View Post
Code:
<?php

// Text file containing words 
$txt = 'list.txt'; 


// function
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;
}

$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."<br>";
foreach ($cwords as $var2) echo $var2."<br>";

?>
Thank you kindly, its appreciated.

Like i said, I'm learning php slowly, i knew there was an issue just couldnt 'see' what it was in my head.

So i was basically screwing up the handling of strip_newlines forcing the array in there, correct?
Webmaster Advertising is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote