Jump to content

Selective adding of a string to an array


etrader

Recommended Posts

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.

<?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);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.