Reputation: 1553
Ive got this bit of code to look in my txt file to see if i already have the item, However it never looks on the first line. Is there something i can do to fix this?
<?php
for($i=0, $count = count($match[1]);$i<$count;$i++) {
$filename = 'alreadyadded.txt';
$searchfor = $match[1][$i];
$file = file_get_contents($filename);
if(strpos($file, $searchfor)) {
echo $match[1][$i]." Is already added, No Actions needed. <br />";
} else {
echo "grabbing this one".$match[1][$i]."<br />";
}
}
?>
Upvotes: 0
Views: 42
Reputation: 1402
This doesn't solve the problem but should make it slightly faster. You don't need to read the file each time.
$filename = 'alreadyadded.txt';
$file = file_get_contents($filename);
for($i=0, $count = count($match[1]);$i<$count;$i++) {
$searchfor = $match[1][$i];
if(strpos($file, $searchfor)!==false) {
echo $match[1][$i]." Is already added, No Actions needed. <br />";
} else {
echo "grabbing this one".$match[1][$i]."<br />";
}
}
Upvotes: 0
Reputation: 5008
You should do it this way:
if(strpos($file, $searchfor) !== false) {
echo $match[1][$i]." Is already added, No Actions needed. <br />";
} else {
echo "grabbing this one".$match[1][$i]."<br />";
}
if the position found is 0 (the first character) you basically get wrong result. You want to compare the result to false including datatype.
0 == false returns true
0 === false returns false
0 != false returns false
0 !== false returns true
Upvotes: 1