Bricktop Posted March 19, 2010 Share Posted March 19, 2010 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 https://forums.phpfreaks.com/topic/195802-display-only-certain-data-from-text-file/ Share on other sites More sharing options...
taquitosensei Posted March 19, 2010 Share Posted March 19, 2010 $text_exp=explode("||", $text); if($text_exp[2]=="category1") { // do whatever here } Link to comment https://forums.phpfreaks.com/topic/195802-display-only-certain-data-from-text-file/#findComment-1028593 Share on other sites More sharing options...
Bricktop Posted March 19, 2010 Author Share Posted March 19, 2010 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 https://forums.phpfreaks.com/topic/195802-display-only-certain-data-from-text-file/#findComment-1028599 Share on other sites More sharing options...
litebearer Posted March 19, 2010 Share Posted March 19, 2010 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 https://forums.phpfreaks.com/topic/195802-display-only-certain-data-from-text-file/#findComment-1028681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.