Jump to content

[SOLVED] two level explode() function.


ted_chou12

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

sorry, my mistake, but there are still some errors:
Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;ttttttttttttttttttttt
Posted on:
Entry Content:
Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;ttttttttttttttttttttt
Posted on:
Entry Content:
Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;ttttttttttttttttttttt
Posted on:
Entry Content:
Subject: tttttttt:separate;readable:separate;17/12/06 3:39 am:separate;ttttttttttttttttttttt
Posted on:
Entry Content:
is what it appears
Link to comment
Share on other sites

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