I feel like the biggest fool on the planet earth. But I am looping through a text file and attempting to compare each line and find the line breaks... You know... " ". But I can't seem to compare the strings!!!! Here is what I am doing.
$filename = 'StatesCities.txt';
$file = file($filename);
$matchedLines = array();
foreach($file as $line) {
$matchesLines[] = $line;
if(strcmp($line, "La Mesa") == 0)
{
echo("Yeah");
}
echo($line);
echo("<br />");
}
Are you trying to remove the line breaks? If that's the case then you can explode them.
$file = file_get_contents( $file_path );
$page = explode( "
", $file );
//Now they're gone
foreach( $page as $line )
{
if( strpos( $line, 'searchfor' ) !== false )
{
//do this
}
}
The strpos is how I usually search in a string, just make sure to use !== which checks for false not false or 0. If the string your looking for has a strpos of 0 ( first character ) it would return a false positive if you don't check for the type with !==. Good luck.
Your question says you're looking for " " but your code is looking for "La Mesa". Perhaps you wanted to say
if (strpos($line, "
"))
?
The file()
function will maintain newline characters () in the array that it creates by default. See here for more info; in particular, the
flags
section. To fix your problem...
Instead of this:
$file = file($filename);
Use this:
$file = file($filename, FILE_IGNORE_NEW_LINES);