ted_chou12 Posted December 17, 2006 Share Posted December 17, 2006 I am using a two layered explode functions, where the php code that is used to divide the data looks like this:[code]<?php$diaryarray = @file("$_SESSION[username]/diaryentries.txt");foreach($diaryarray as $v){$diaryentry = explode("#",$v);foreach($diaryentry as $w){list($subject,$entryicon,$datetime,$content) = explode(":separate;",$w);echo "<br><img src=\"smilies/$entryicon.gif\"><font face=\"arial\" size=4><b>Subject: $subject</b><br><b>Posted on: $datetime</b><br><b>Entry Content:</b> $content</font><br>\r\n";}}?>[/code]and the data piece looks like this:[code]subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#subject:separate;image:separate;17/12/06 3:39 am:separate;content#...[/code]and goes on forever, but it doesnt seem to work, could anyone help me with this?thanks again Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/ Share on other sites More sharing options...
redbullmarky Posted December 17, 2006 Share Posted December 17, 2006 when you say "it doesnt work", can you explain what exactly it is you're trying to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142853 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 sorry for being brief, by "it doesnt work" i mean that it only appears the first level, which is the one divided by #, but doesnt appear the details of each eg. the subject, datetime, content, picture.If you need further details please inform me.thanks Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142880 Share on other sites More sharing options...
heckenschutze Posted December 17, 2006 Share Posted December 17, 2006 Use something like:[code]$aL= explode('#', $data);$iNumL = count($aL);for($i = 0; $i < $iNumL; $i++){ $aLine = explode(':', $aL[$i]); echo "Subject = " . $aLine[0];}[/code]Note, I prefer [color=pink]for[/color] over [color=pink]foreach[/color] (due to foreach is mainly a PHP construct and often not found in other similar languages like C) -> it also gives you more control on how the array is processed. Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142891 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 okay thanks, i will try it. Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142892 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 but i dont quite understand your string and variables, can you tell me which ones to replace please?thanks Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142893 Share on other sites More sharing options...
heckenschutze Posted December 17, 2006 Share Posted December 17, 2006 Just replace $data with @file("$_SESSION[username]/diaryentries.txt"); ...example:[code]$aL= explode('#', file("$_SESSION[username]/diaryentries.txt"));$iNumL = count($aL);for($i = 0; $i < $iNumL; $i++){ $aLine = explode(':', $aL[$i]); echo "Subject = " . $aLine[0];}[/code]and format/use $aLine how you wish, note: $aLine will look something like:[code]array( 0 => "subject", 1 => "separate;image", 2 => "separate;17/12/06 3:39 am", 3 => "separate;content");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142895 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 thanks mate. ;D Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142897 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 would this be it?:[code]<?php$aL= explode('#', file("$_SESSION[username]/diaryentries.txt"));$iNumL = count($aL);for($i = 0; $i < $iNumL; $i++){ $aLine =array( 0 => "$subject", 1 => "$entryicon", 2 => "$datetime", 3 => "$content"); echo "Subject = " . $aLine[0];}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142898 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 nope... doesn't work :'( :'(, i need further help... please?EDITED: by doesnt work i mean it only appears "subject=" and nothing else Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142902 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 can anyone please help me with this, im going no where...havnt made any progress on this!!! :'( :'( Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142932 Share on other sites More sharing options...
kenrbnsn Posted December 17, 2006 Share Posted December 17, 2006 Just saw the topic...First, a clarification question. In your data, in looks like the '#' is at the end of each line with nothing after it? If that is true, you don't need the first explode, just trim the '#' from the string. So you code should look something like this:[code]<?php$diaryarray = file($_SESSION['username'] . '/diaryentries.txt') or die ('Problem reading file');foreach($diaryarray as $v) { list($subject,$entryicon,$datetime,$content) = explode(":separate;",trim(trim($v),'#'); // the inner trim gets rid of the newline character at the end of the line, the outer one gets rid of the '#' echo '<br><img src="smilies/$entryicon.gif"><font face="arial" size=4><b>Subject: ' . $subject . '</b><br> <b>Posted on: ' . $datetime . '</b><br><b>Entry Content:</b> ' . $content . '</font><br>' . "\r\n";}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142944 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 hey, thanks for your reply, the # is what i use to divide the data, so when explode, subject#time#image#content can be separated. Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142955 Share on other sites More sharing options...
kenrbnsn Posted December 17, 2006 Share Posted December 17, 2006 Going back to your original post, you said the data looks like:[code]subject:separate;image:separate;17/12/06 3:39 am:separate;content#[/code]That's what I based my answer on. If the data does not look like that, my assumptions are not correct.Ken Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142957 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 [b]EDITED[/b]: There is another php page which it writes into the txt files, and I wrote with # originally in order to use the explode() function with it, however, if you dont wish that to be there I can also modify my other php page to fit to your script.so that ...#...#...#... is what i previously wrote into the txt file, if to your code's convenience, you dont wish those there, just ignore them, I will write the file in the way you want (but can you give me an example as to how you want the txt file to look like?)thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142958 Share on other sites More sharing options...
kenrbnsn Posted December 17, 2006 Share Posted December 17, 2006 As the script's author, you make the decisions as to what your data looks like. I only make suggestions on how to accomplish what you want.If you are using the '#' as a separator, then my code would look something like:[code]<?php$diaryarray = file($_SESSION['username'] . '/diaryentries.txt') or die ('Problem reading file');foreach($diaryarray as $v) { list($subject,$entryicon,$datetime,$content) = explode("#",trim($v)); echo '<br><img src="smilies/$entryicon.gif"><font face="arial" size=4><b>Subject: ' . $subject . '</b><br> <b>Posted on: ' . $datetime . '</b><br><b>Entry Content:</b> ' . $content . '</font><br>' . "\r\n";}?>[/code]BTW, sending PM's won't get answers more quickly.Ken Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142960 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 thanks, but does anyone know what the . means? and there are syntax errors, so I want to ask if there are any differences between single quotes and double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142963 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 sorry, my mistake, but there are still some errors:Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;tttttttttttttttttttttPosted on: Entry Content: Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;tttttttttttttttttttttPosted on: Entry Content: Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;tttttttttttttttttttttPosted on: Entry Content: Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;tttttttttttttttttttttPosted on: Entry Content: is what it appears Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142965 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 okay, after putting the two together, its solved, thanks ken. Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142969 Share on other sites More sharing options...
ted_chou12 Posted December 17, 2006 Author Share Posted December 17, 2006 btw sorry for sending you those pms, i was eager to get the job done... Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-142980 Share on other sites More sharing options...
redbullmarky Posted December 17, 2006 Share Posted December 17, 2006 ted, i dont want to sound out of line or dismissive here, but I'd like to point you to two things, both of which are stickied at the top of the PHP Help forum. 1) the forum posting guidelines - including not pushing your problem excessively or expressing urgency. people here help for free. if you need help quicker, the freelance board (for paid help) maybe more useful to you - else patience would be appreciated. i've told you about this before though you seem to have forgotten. unless you're gonna pay - everybody's post here is equally important.2) the PHP FAQ thread, also pinned at the top of the PHP Help forum, is worth a read, as most/many of the problems you're having is covered in there. Many of the scripts you're using are scripts that people have written for you over several other posts - so at least have the brain to try and understand how they work before dismissing them as broken - and most of the answers are in the FAQ or [url=http://www.php.net/manual]the manual[/url]. Helping yourself and thinking of your problem a little first will go a long way, especially for your future. Sure, if after all that you still cant find an answer, PHP Help is your place.again, i dont want to be unnecessarily out of line here, but it's not like i havent mentioned it before to you - trying to help yourself out a little first, and doing a bit of your own research will go a LONG way.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/30955-solved-two-level-explode-function/#findComment-143029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.