I need help splitting data from a .txt file into two pieces.

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]$location = ‘/file.txt’;
$data = file($location);
$line = trim($data[array_rand($data)]);
list($a, $b) = preg_split("+", $line, 2);
echo $a,$b;[/php]

In the text file it looks like this

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…

[b]Warning[/b]: preg_split() [function.preg-split]: No ending delimiter '+' found in [b]/public_html/index.php[/b] on line [b]44[/b]

If you could help a newbie out it would be much appreciated! :wink:
~ 123z

Hi there,

Try the following code:


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


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

Sponsor our Newsletter | Privacy Policy | Terms of Service