Jump to content

Display only certain data from text file


Bricktop

Recommended Posts

Hi all,

 

This is probably really easy but I just can't figure it out!

 

I have a text file which has data stored exactly like:

 

some text || some name || category1

some text2 || some name2 || category1

some text3 || some name3 || category1

some text4 || some name4 || category2

some text5 || some name4 || category2

some text6 || some name6 || category3

some text7 || some name7 || category1

 

I'm using the following simple code to read all of the data into a variable:

 

$text = explode("\n", $textcontent);

 

$textcontent is defined and used earlier on just to fread the file.

 

How do I only store in the $text variable lines of text with 'category1' (for example)?

 

I assume I then need to 'explode("||", $text);' so I've got everything split into the relevant chunks but I'm stuck after that.  I very rarely work with text files, this would be so easy in MySQL!

 

Thanks in advance!

Link to comment
Share on other sites

Hi taquitosensei and thanks but I already tried that and when that 'if' condition doesn't get met nothing displays.  I'll keep trying to get it going but thanks anyway.

 

What I need is for it loop around again if that condition isn't met, until it is met.

 

Any ideas?

Link to comment
Share on other sites

This may help..

 

<?
$textcontent[0] = "some text 1|| some name 1||category1";
$textcontent[1] = "some text 2|| some name 2||category3";
$textcontent[2] = "some text 3|| some name 3||category6";
$textcontent[3] = "some text 4|| some name 4||category1";
$textcontent[4] = "some text 5|| some name 5||category5";
$textcontent[5] = "some text 6|| some name 6||category9";
$textcontent[6] = "some text 7|| some name 7||category1";



$total_lines = count($textcontent);

$i=0;
$x=0;
While ($i<$total_lines) {
$line_content_array = explode("||", $textcontent[$i]);
if ($line_content_array[2] == "category1") {
$keep_line[$x] = $textcontent[$i];
$x = $x + 1;
}
$i = $i + 1;

}
$i = 0;
$x = count($keep_line);
While($i<$x) {
  echo $keep_line[$i] . "</br>";
  $i = $i + 1;
}
?>

 

NOTE: the value of YOUR data has a space in front of 'category1'; therefore you need to either remove the space or add the space to your 'needle' (ie  "category1" does NOT equal " category1")

 

Make sense?

 

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.