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. Link to comment https://forums.phpfreaks.com/topic/225886-selective-adding-of-a-string-to-an-array/ 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? Link to comment https://forums.phpfreaks.com/topic/225886-selective-adding-of-a-string-to-an-array/#findComment-1166190 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); ?> Link to comment https://forums.phpfreaks.com/topic/225886-selective-adding-of-a-string-to-an-array/#findComment-1166193 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); Link to comment https://forums.phpfreaks.com/topic/225886-selective-adding-of-a-string-to-an-array/#findComment-1166195 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.