etrader Posted January 27, 2011 Share Posted January 27, 2011 I have a file containing lines (I mean elements separated by <br>). How I can put these lines into an array, but only those which has a given phrase. Example file This is the first line<br> Second line is here<br> something else<br> something else<br> something more<br> I want to catch only lines which contain the word "something" and make an array. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted January 27, 2011 Share Posted January 27, 2011 does the file also have linebreaks ("\r" or "\r\n") Does it look like this. This is the first line<br>Second line is here<br>something else<br>something else<br>something more<br> or does it look your example? Quote Link to comment Share on other sites More sharing options...
codefossa Posted January 27, 2011 Share Posted January 27, 2011 <?php $string = "This is the first line<br>Second line is here<br>something else<br>something else<br>something more<br>"; $array = explode("<br>", $string); $new_array = array(); foreach ($array as $line) { if (strstr($line, "something")) array_push($new_array, $line); } print_r($new_array); ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted January 27, 2011 Share Posted January 27, 2011 preg_match_all('/(.*something.*)<br>/', $file_contents, $matches); print_r($matches); Quote Link to comment 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.