10 Replies - 6569 Views - Last Post: 19 March 2009 - 02:20 AM Rate Topic: -----

#1 tiff88   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 09-July 08

php, explode() function

Post icon  Posted 03 December 2008 - 06:22 PM

Hi all,

another question from me ;-). I want to use the explode function to separate a text file that I read into a string, into an array. So, looking at the little example text file right below:
Bubba, Comps, 12/23/1984
Ahmed, Allthat, 04/12/1979



If i take the above text file and run the following code on it
$filename = "data.txt";
$fp = fopen($filename, "r" ) or die("Could not open file");
$contents = file_get_contents($filename);
fclose($file);
$delimiter = ",";
$splitcontents = explode($delimiter, $contents);
$counter = "";
print $splitcontents[2];



So, once I get to splitcontents[2] i get the following printout
12/23/1984 Ahmed


I want the birthdate and the name separate. My question is, can I include an additional delimiter, or how do I get it working so all first names, last names, and birth dates are in separate array slots. Thanks again for all the help and advice.

Is This A Good Question/Topic? 0
  • +

Replies To: php, explode() function

#2 Valek   User is offline

  • Gravity seems weak until you look down
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,716
  • Joined: 08-November 08

Re: php, explode() function

Posted 03 December 2008 - 06:27 PM

Well, since you're using a comma as the delimiter, you could add a comma at the end of every line, then use ltrim() if you get whitespace at the start of some of your results.

That's the easy answer though. If you absolutely cannot do that, post so and I'll let you know the harder way to do it.
Was This Post Helpful? 1
  • +
  • -

#3 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: php, explode() function

Posted 03 December 2008 - 06:28 PM

you either need to put a , on the end of lines, or split on \n too. New lines are not handled when exploding on , only.
Was This Post Helpful? 1
  • +
  • -

#4 ericr2427   User is offline

  • D.I.C Regular
  • member icon

Reputation: 40
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Re: php, explode() function

Posted 03 December 2008 - 06:29 PM

In the text file try removing the spaces after the commas, or setting the delimeter to ", "
Probably won't work considering I've never used that function...
Was This Post Helpful? 0
  • +
  • -

#5 tiff88   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 09-July 08

Re: php, explode() function

Posted 03 December 2008 - 07:00 PM

Thanks for all the advice.
I ended up doing it like this
$fp = fopen($filename, "r" ) or die("Could not open file");
			 while ( !feof( $fp)){
				$line = fgets( $fp, 255 );
				$line = chop( $line );
				$field = split( ",", $line, 3 );
				echo $field[0];
				echo $field[1];
				echo $field[2];
				echo "<br>";
			}



Now, how would i put this data into an html table. Can i just mix html in and use the php variable names in the html part?
Was This Post Helpful? 0
  • +
  • -

#6 tiff88   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 09-July 08

Re: php, explode() function

Posted 03 December 2008 - 07:07 PM

for example,
would something like this work
 echo <a href ="$field[2]"website link</a>";


Was This Post Helpful? 0
  • +
  • -

#7 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: php, explode() function

Posted 03 December 2008 - 07:34 PM

you would need to have proper html format
echo "<a href='".$field[2]."'>link</a>";
or
?><a href="<?php echo $field[2]; ?>">link</a><?php
swapping in and out of php.
A similar approach can be used for tables.
Was This Post Helpful? 0
  • +
  • -

#8 ericr2427   User is offline

  • D.I.C Regular
  • member icon

Reputation: 40
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Re: php, explode() function

Posted 03 December 2008 - 07:34 PM

Never mind, can't compete with Wilson :)

This post has been edited by ericr2427: 03 December 2008 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#9 Valek   User is offline

  • Gravity seems weak until you look down
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,716
  • Joined: 08-November 08

Re: php, explode() function

Posted 03 December 2008 - 07:41 PM

foreach($field as $key => $val) {
  echo "<a href='link here'>{$val}</a><br />\r\n";
}


The code above would work if you wanted a list of links straight down (judging by the example you posted) with the text on the links equivalent to the value of each entry in $field.

And as William_Wilson said above, a similar approach can be used with tables.

This post has been edited by Valek: 03 December 2008 - 07:42 PM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: php, explode() function

Posted 03 December 2008 - 07:52 PM

Beating a dead horse:

You can read the entire file in one shot, assuming it isn't so large that you'll run out of memory. ;)

<?php
$mytemp = readmyfile("Thefile.txt");
$mytext = str_replace("\n",",",$mytemp);
$myarray = explode (",",$mytext);

var_dump($myarray);

function readmyfile($thefile)
		{	
		$myfile=fopen($thefile,"r");
		$x = fread($myfile, filesize($thefile));
		fclose($myfile);
		return $x;
		}
?>

Was This Post Helpful? 0
  • +
  • -

#11 vipulpatel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-March 09

Re: php, explode() function

Posted 19 March 2009 - 02:20 AM

Well, You try space as delimeter and then fetch the 0 position in array. you will get solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1