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.

Link to comment
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.