Author Topic: I need help splitting data from a .txt file into two pieces.  (Read 250 times)

123z

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
I need help splitting data from a .txt file into two pieces.
« on: September 03, 2010, 01:53:33 PM »
Hi, I'm new to the community.

I'd like some help on splitting data from a .txt file into two pieces.

This is what I have so far.
PHP Code: [Select]
$location '/file.txt';
$data file($location);
$line trim($data[array_rand($data)]);
list(
$a$b) = preg_split("+"$line2);
echo 
$a,$b;


In the text file it looks like this
Code: [Select]
Peter+1200 Griffin Avenue
Luis+1553 Atlantic Blvd
Zac+4500 Island Blvd
and so on...

The script is suppose to grab a random line and split that line into two variables, a and b, so then $a and $b can be used on that page in different areas..

So far it seems that preg_split isn't grabbing the information from the text file but looking in the same page (index.php)
the error I'm getting..
Quote
Warning:  preg_split() [function.preg-split]: No ending delimiter '+' found in /public_html/index.php on line 44

If you could help a newbie out it would be much appreciated!  ;)
~ 123z
« Last Edit: September 03, 2010, 01:55:04 PM by 123z »

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 506
  • Karma: +4/-0
    • View Profile
Re: I need help splitting data from a .txt file into two pieces.
« Reply #1 on: September 08, 2010, 04:35:47 PM »
Hi there,

Try the following code:

PHP Code: [Select]

<?php
$filename 
"./info.txt";
$file file($filename);
foreach(
$file as $data)
{
    
$info explode("+",$data);
    echo 
"Name = ".$info[0];
    echo 
"<br />";
    echo 
"Address = ".$info[1];
    echo 
"<hr />";
}
?>


Obviously just change the filename to the location and filename that you are using. Let me know how it goes!