Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP script


ghost's Avatar
0 0

Im trying to write a script that searches a text file to see if a word is in it. :::Things to note:::

I've only started learning php today $search is the string that the user enters into the text box. Textfile format is line 1 line 2 line 3 (If that makes a difference.

:::Here is my code:::

<?php

$wordlist="words2.txt"; $list = file ($wordlist); foreach ($list as $word) { if ($search == $word) { echo "Found!"; } }

?>

It doesn't search it line by line, checking each line at a time (Which is what i want). It only compares $search with the last word in the text file.

Can anyone help me so it will search for any word in the file, not just the last one?

Any help would be appreciated extremely :)


ghost's Avatar
0 0

Well personally I would use a regular expression (regex) and then I could do preg_match_all() but thats just me


ghost's Avatar
0 0

Haven't done anything like this, just started PHp a while ago but you could use the "explode" command to make the file an array made up of individual words and check them


ghost's Avatar
0 0

I have to agree with mozzer with the regex thingy. Regexes are slow in some cases but when you're handling bigger files you won't regret using them.

About the explode function, if you have a 3000 word text than the zend engine would need to preserve memory for an array of 3000 strings. Which isn't this performant ;)


ghost's Avatar
0 0

Well the point is when your reading a file line by line there is always the caracter "\r\n" or "\n" at the end. So if you want to compare it with a word add $word = str_replace(Array("\r","\n"),"",$word); and this should be ok.


ghost's Avatar
0 0

<?php

$wordlist="words2.txt"; $list = file ($wordlist); foreach ($list as $word) { if ($search == $word) { echo "Found!"; } }

?>

so if you wanted to seperate every thing by spaces, then that would seperate the words. soo you could change it to

&lt;?php

$wordlist=&quot;words2.txt&quot;;
$list = file ($wordlist);
foreach ($list as $search {
list($search)=split(&quot; &quot;, $search);
if($search==$word){
echo &quot;Found!&quot;;
}
}

?&gt;

ghost's Avatar
0 0

Thanks everyone for your help. I didn't realize it added "/r/n" to every word, just had to take that off and it fixed it (Thanks Arto_8000).

Just in case you were wondering a was trying to make an md5 dictionary cracker. I know most wouldn't be interested, but i thought id post the code. It probably could be tidier, but hey.


&lt;?php

$wordlist=&quot;words.txt&quot;;
$list = file ($wordlist);
$search = strtolower($search);
$search .= &quot;&quot;;
foreach ($list as $word) {

$word = substr_replace($word,&quot;&quot;,-1);

if ($search == md5(substr_replace($word,&quot;&quot;,-1))) {
	echo &quot;&lt;i&gt;$search&lt;/i&gt;&quot;, &quot;=&quot;, &quot; &quot;,&quot;&lt;b&gt;$word&lt;/b&gt;&quot;, &quot; - Successfully cracked!&quot;;
$nocrack = 1;
}
}

if ($nocrack ==0) {
	echo &quot;Crack unsuccessful!&quot;;
	}

?&gt;

My very first php script, yay!


ghost's Avatar
0 0

Well actually there are a few PHP scripts for MD5 dictionary crackers in the code-bank so you could've just gotten ideas from them


ghost's Avatar
0 0

Happysmileman wrote: Well actually there are a few PHP scripts for MD5 dictionary crackers in the code-bank so you could've just gotten ideas from them

Thanks for your input, but I didn't copy any md5 cracker scripts - bit of jealous maybe?


richohealey's Avatar
Python Ninja
0 0

i think what he was saying is that you could have looked at the code back, and seen the other scripts, and used them to find your bug.


richohealey's Avatar
Python Ninja
0 0

i think what he was saying is that you could have looked at the code back, and seen the other scripts, and used them to find your bug.