PHP split delimited text variation

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I have a text document with separated values, but the delimiter is the pipe (OR logical operator |). So i found to have php not to read | as an operator to just escape it and that my script to atleast run without errors. However, I've used the script in the past to read separated values and put in a database, but I can't get it to work with the pipe delimiter text file.

I think I just need a way to use str replace to remove all the | from the text file and replace with commas and then use my script.


Code: [ Select ]

$fname="item.txt";
$fz = filesize($fname);

$fp=fopen($fname,"r") or die("Error found.");
$line = fgets( $fp, $fz );
while(!feof($fp))
{
list($item_num,$description,$unitmsr,$wgt,$stdpk) = split( "\|", $line, 5 );
$item_num2 = trim($item_num);
$description2 = trim($description);
$unitm = trim($unitmsr);
$weight = trim($wgt);
$std = trim($stdpk);
$qry = "INSERT INTO db...";
$result = mysql_query($qry);
$line = fgets( $fp, $fz );
}
fclose($fp);
  1. $fname="item.txt";
  2. $fz = filesize($fname);
  3. $fp=fopen($fname,"r") or die("Error found.");
  4. $line = fgets( $fp, $fz );
  5. while(!feof($fp))
  6. {
  7. list($item_num,$description,$unitmsr,$wgt,$stdpk) = split( "\|", $line, 5 );
  8. $item_num2 = trim($item_num);
  9. $description2 = trim($description);
  10. $unitm = trim($unitmsr);
  11. $weight = trim($wgt);
  12. $std = trim($stdpk);
  13. $qry = "INSERT INTO db...";
  14. $result = mysql_query($qry);
  15. $line = fgets( $fp, $fz );
  16. }
  17. fclose($fp);


Many thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

What's the problem exactly? It doesn't work where? What doesn't work like it should? What kind of error do you get?
  • neksus
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2194
  • Loc: Canada

Post 3+ Months Ago

$replace = ",";
substr($fname, |, $replace);
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

neksus wrote:
$replace = ",";
substr($fname, |, $replace);

I don't think you got the right function... shouldn't it be strtr or str_replace or str_ireplace? Substr is to shortening a string of text...
  • neksus
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2194
  • Loc: Canada

Post 3+ Months Ago

Sorry, you're right. That's what happens when you try and do a bunch of different languages at the same time. Stupid homework.
str_replace() is the correct function.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

neksus wrote:
Sorry, you're right. That's what happens when you try and do a bunch of different languages at the same time. Stupid homework.
str_replace() is the correct function.

:lol: it happens and I think there should be a quote around the pipe in the function str_replace($fname, '|', $replace);
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Since split takes a regular expression as an argument, the pipe is considered a special character.

Only use the split function when you need to split a string based on a pattern. Come to think of it, I've never needed to use the split function, preg_match has always done just fine where patterns are involved. The manual even suggests using preg_split when patterns are involved.

For single character, or non-varying delimiters, use the explode function instead.

I wouldn't be surprised if the split function were to become depreciated down the road.

Now, looking at that script I have to wonder about the server setup involved and what access levels you have.
It looks like MySQL servers native LOAD DATA INFILE syntax used through a command-line client could be a better alternative since it supports dynamic delimiters.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

this site is great.

I got an answer plus something else to look into from joebert.

The string replace should fix it as I can change it to comma separated values.

Sorry Bogey about the error response. It seems it never really kicked me an error but rather just didn't populate my db. Sorry I was so vague on my problem. All I knew is the script acted differently using the pipe instead of commas.

thanks for the help.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 129 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.