Hi there,
I've got a text file - short amount of it listed below:

if
then
and
why
how
hi
where
i
am

Now in my script it reads it as a file then explodes it into an array - with the seperator being "\n".

That works fine.

However, when I check a word to see if it exists in the array of words above, it doesn't work.

array_search('hi', $this->WordList)

Now that says it doesn't exist in the array (yet it does). However, if I do this:

array_search('am', $this->WordList)

(last word in the array) - it works fine.

Any suggestions?

Thanks,

Chris Evans

    It sounds like you have a new line problem.
    Which operating system are you using?

      Hi,
      I'm on Windows... and apologies, I meant I was using \r\n not \n. It's definately a working array... I'm using the following code:

      	// Open the list of dropped words and parse to an array
      	function listToArray() {
      		// Location of the file
      		$location = '../includes/wordlist.txt';
      
      	// Open the file
      	$fp		= fopen($location, 'r');
      	$size	= filesize($location);
      	$read	= fread($fp, $size);
      
      	// Now read each word into a array
      	$this->WordList = explode("\n", $read);
      }
      

      To import it.

      When I print_r the array, I get:

      Array ( [0] => if [1] => then [2] => and [3] => why [4] => how [5] => hi
      

      etc.

      Thanks,

      Chris

        I meant I was using \r\n not \n. It's definately a working array... I'm using the following code:

        Well, you may claim you're using \r\n, but you arent.
        explode() with "\r\n"

        Conversely, you could read the file with file(), then remove the trialing newline sequences with rtrim().
        This would be more portable.

          :eek: how embarrasing!

          		// Remove the punctuation, lines, and extra spaces
          		$this->Text = strtolower(str_replace($punctuation, ' ', $this->Text));
          		$this->Text = eregi_replace("\r\n", " ", $this->Text);
          		$this->Text = eregi_replace("\n", ' ', $this->Text);
          		$this->Text = eregi_replace(" +", " ", $this->Text);
          

          I was reading these lines which actually had nothing to do with this part of the script! 🙁

          Woops. Thanks for pointing that out. 🙂 Works fine now.

          Chris

            Write a Reply...