mraza Posted November 3, 2010 Share Posted November 3, 2010 Hi I need to read a file and if a line match then proceed, file have content like this file.txt this is line one this line two and this line three my code: $content = file_get_contents("file.txt"); if (strstr("this line two", $content)) { echo 'matched'; } else { echo 'not matched'; } but this always give not matched. any help plz Quote Link to comment https://forums.phpfreaks.com/topic/217678-match-a-line-in-txt-file/ Share on other sites More sharing options...
BlueSkyIS Posted November 3, 2010 Share Posted November 3, 2010 you could read the file into an array and loop over the elements of that array, looking for matches: $content = file('file.txt'); foreach ($content AS $aline) { if (strstr("this line two", $aline)) { echo 'matched'; } else { echo 'not matched'; } } Quote Link to comment https://forums.phpfreaks.com/topic/217678-match-a-line-in-txt-file/#findComment-1129960 Share on other sites More sharing options...
mraza Posted November 3, 2010 Author Share Posted November 3, 2010 thanks for help , but that did not worked either, when i did a print_r($content) this what i get Array ( [0] => this is line one this line two and this line three ) so all three lines it put in one array element, then i even tried $c = explode("\r\n", $content); but again it shows me above results. Quote Link to comment https://forums.phpfreaks.com/topic/217678-match-a-line-in-txt-file/#findComment-1129981 Share on other sites More sharing options...
salathe Posted November 3, 2010 Share Posted November 3, 2010 Show us the code that you're really using. Quote Link to comment https://forums.phpfreaks.com/topic/217678-match-a-line-in-txt-file/#findComment-1129984 Share on other sites More sharing options...
mraza Posted November 3, 2010 Author Share Posted November 3, 2010 thanks here is exactly file.txt this is line one this line two and this line three and this mycode.php <?php $content = file('file.txt'); // i tried like this too $c = explode("\r\n",$content); print_r($content); foreach ($content AS $aline) { if (strstr("this line two", $aline)) { echo 'matched'; } else { echo 'not matched'; } } /* // Result with print_r($content) Array ( [0] => this is line one this line two and this line three ) not matched // Result with print_r($c) Array ( [0] => Array ) not matched */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/217678-match-a-line-in-txt-file/#findComment-1129989 Share on other sites More sharing options...
mraza Posted November 3, 2010 Author Share Posted November 3, 2010 ok this works $content = file_get_contents('file.txt'); $content = explode("\r\n",$content); print_r($content); foreach ($content AS $aline) { if (strstr($aline, " this is line three")) { echo 'matched'; } else { echo ' not matched'; } } Quote Link to comment https://forums.phpfreaks.com/topic/217678-match-a-line-in-txt-file/#findComment-1130004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.