Jump to content

how do you number the explode()?


ted_chou12

Recommended Posts

I want to know how you can number the explode() for example, the first explode would be one, second two and third three. It goes on as depedent to how many explode there are. The aim of this is to make editing the specific explode easier, I want to be able to edit or delete the specific explode term. Below is my code, so that you can see how it might work out for me.
[code]
<?php
$diaryarray = file($_SESSION['username'] . '/diaryentries.txt');
foreach($diaryarray as $v) {
      list($subject,$entryicon,$datetime,$content) = explode(":separate;",$v);
      echo '<br><table width=\"100%\" bgcolor="#ffffff"><tr><td valign=top width="100"><b>Entry Icon:</b></td><td width="600"><img src="smilies/' . $entryicon . '.gif"></td></tr><tr><td valign=top><font face="arial" size=4><b>Subject: </b></font></td><td><font face="arial" size=4><b>' . $subject . '</b></font></td></tr><tr><td valign=top><font face="arial" size=4><b>Posted on:</b></font></td><td valign=top><font face="arial" size=4><b>' . $datetime . '</b></font></td></tr><tr><td valign=top><font face="arial" size=4><b>Entry Content:</b></font></td><td><font face="arial" size=4>' . $content . '</font></td></tr></table>' . "\r\n";
}
?>
[/code]
Thanks Ted.
Link to comment
https://forums.phpfreaks.com/topic/31336-how-do-you-number-the-explode/
Share on other sites

explode() creates an array so by default you can access them using numbers starting at 0
[code]
$string='this.is.a.string';

$strings=explode('.',$string);

echo $strings[0].'<br>';
echo $strings[1].'<br>';
echo $strings[2].'<br>';
echo $strings[3].'<br>';

[/code]
produces

this
is
a
string
hey, thanks, I understand. :D but instead of numbering each string in the set, for my two level explode, can you number the sets, by that I mean, each $username, $name, ... is a set, so you can echo something like set five username or set10 name.
Do you understand what I am talking about??? if not i will explain it to you
Thanks
Ted.
yes the file() method produces an array.
[code]

$diaryentry[0] // this will produce the first line of the file
$diaryentry[1] // this will produce the second line of the file

[/code]

I hope this helps, if not post an example of what is contained in the txt file so I can see what you are trying to explode

nate
okay, this is how my txt files roughly looks like:
[code]
subject1:separate;picture1:separate;time1:separate;content1
subject2:separate;picture2:separate;time2:separate;content2
subject3:separate;picture3:separate;[color=red]time3[/color]:separate;content3
subject4:separate;picture4:separate;time4:separate;content4
subject5:separate;picture5:separate;time5:separate;content5
[/code]
where ":separate;" is what I use to divide my data into pieces, what I wish to know is how do you call a specific data piece, such as I want the time of data set 3 (which is colored in red), or anyother else?
do i need to put this "$strings[2]:separate;'<br>';" and this "$diaryarray[3];" together? if so, how do I put them together?
Thanks, Ted.
[code]<?php

//set $filename to the filename the data is in
function explode_lines ($filename)
{
$lines = file($filename);
foreach($lines as $num => $line)
$lines[$num] = explode(":separate;", $line);
return $lines;
}

$data = explode_lines("filename.txt");
echo $data[0][0]; //will echo the subject in the first line
echo $data[4][3]; //will echo the content of line 5
echo $data[12][1]; //will echo the picture of line 13

?>[/code]

Orio.
I wish to combine this:
[code]
<?php
$files = explode("|",$_GET['id']);
foreach ($files as $include)
include ($include.".html");
?>
[/code]
so that you can have a unique url for each piece of data in a text file.
For example, if I have "page.php?id=6" I will have a page including the 6th line of the text file, can anyone help me to do that please?
Thanks Ted.

Archived

This topic is now archived and is closed to further replies.

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