PHP script
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 :)
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 ;)
<?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
<?php
$wordlist="words2.txt";
$list = file ($wordlist);
foreach ($list as $search {
list($search)=split(" ", $search);
if($search==$word){
echo "Found!";
}
}
?>
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.
<?php
$wordlist="words.txt";
$list = file ($wordlist);
$search = strtolower($search);
$search .= "";
foreach ($list as $word) {
$word = substr_replace($word,"",-1);
if ($search == md5(substr_replace($word,"",-1))) {
echo "<i>$search</i>", "=", " ","<b>$word</b>", " - Successfully cracked!";
$nocrack = 1;
}
}
if ($nocrack ==0) {
echo "Crack unsuccessful!";
}
?>
My very first php script, yay!