Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Comma Delimited File


I am trying to read comma delimited rows of text. The problem is that
some fields may be encapsulated in "" - particularly the text fields but
not numeric fields.

Is there a simple efficient way to parse the fields with comma but also
strip off the "" encapsulating some of the fields with php?

The problem is the "" encapsulation is optional. Some fields will have
it, some won't.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Stripping Quotes Out Of A Comma Delimited File?
I have a flat file that I'm trying to stick into a MySQL database. One
record per line, multiple fields per record, and many of them are null
fields which are just double quotes without a space between. It's probably
nothing really major for people who have done this before, but I'm a bit
stumped. The file is comma delimited. Every field is surrounded by double
quotes. I've done quite a bit of searching, on the php site and elsewhere,
but I can't seem to get it to strip the quotes out so I can explode the file
line by line to grab the fields.

Comma Delimited Csv Has Commas Inside Fields
I need to import a CSV file into a database daily. That operation in itself is simple enough but the trouble is that some fields are quoted because they have commas inside them. How do I handle this with my import script?

It would be much better if the CSV was created with all fields quoted or used a different delimited but sadly that is not an option.

Regular Expression For Comma-delimited Pairs
I have a textarea form field for inputting (or pasting) pairs of data.

I need a regular expression pattern to validate each line for the following

double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return

The following comes close, but doesn't check for a carriage return at the
end of each line:

^"([0-9]?)+"([,]s?"([A-Za-z0-9]+)")*$

For example the following would return true:

"1","John"
"2","Paul"
"3","George"
"4","Ringo"

Reading Excel To MySql Or Comma Delimited ...
right direction on how to deal with an xls
flie.

but the only other file I have to work
with is pdf.

I'm not sure, is xls the better of two evils?


Easy Comma-delimited List To Dropdown Box Converter
I was working on a simple converter when I realized that by not setting the id param of the option tag, it wasn't going to send the value of the dropdown box to my php script. I had this to start with: Code:

Delimiting A Non-delimited File
I have a text file, with addresses of business' that I would like to convert to a more useable format ie CSV. Currently there is no delimiting of the various address components.

"A 1 AUTO SALES 1224 S BROADWAY ST WICHITA, KS 67211-3124 316-263-8748"
"ABERLE FORD INC 1025 MAIN ST SABETHA, KS 66534-1893 913-284-3122"
"ACTION AUTO RENTAL & SALES 1101 W 4TH AVE HUTCHINSON, KS 67501 316-662-3699"

As you can see from the examples above the only consistent items are the state and the phone number, and the city is always followed by a comma. I am new to regex and the whole range of string handling functions and I am not sure where to start.

Tab Delimited Txt File To Php Html Table
Wouldn't you know that when I finally find a tutorial using Google that does exactly what I want, the code doesn't run when I use it ...

However, when it gets to this line, for ($i=0; $i<td>$line[0]</td>, it chokes on one of the '>'.

Since my php skills are very basic, and in no way include arrays, can someone assist me with this?

If it's easier to do it differently, what I'd like to do is create a table in Excel/Access, export it to the server (to a tab-delimted text file, since I'll be using commas in my table cells), and have it display in an HTML table.

If I can't figure this out, I'm tempted to just export to HTML from one or the other program (not sure quite yet which I would use), so ...

Reading Tab Delimited File Into Database
I have a .tab file that I need to write to the database. I've read it in, but for some reason it's not reading correctly. I'm truncating the complete database and then reloading it with whatever is in the tab file. I keep getting an error when trying to run my current code which is: Code:

Match Input Value With Values In A Delimited File
I'm very very new to php. I need your help. I have a text file which is ~ delimited. Each line has a name and link. I want to check if the link is similar to my input and if it is then return the name.

Importing To DB From Pipe Delimited Text File
I am trying to make a PHP script that will import data from a pipe delimited text file in to a database. The problem I am having is that the fields have long descriptions in them and there are multiple line breaks for paragraphs. They contain HTML codes as well (<B>).

The multiple line breaks are causing it to import the data strange, putting data in the wrong fields and totally missing some data as well. Here is what the text file Code:

Multiple Table Insert From Tab Delimited File
I have a page I'm coding for batch updates from a tab delimited file. Each row has data that has to be checked against and inserted/updated on different tables in my mySQL database.

How would I go about parsing through the tab delimited file and then storing data between the tabs in order to insert individually into separate tables??

Here's what I have so far...

$contents = file('/myfile.txt');

for ($i=0; $i<sizeof($contents); $i++) {

$line = trim($contents[$i]);
$arr = explode(" ", $line);

$line[$i] = array('ID' => $arr[0], 'Record_Type' => $arr[1], 'First_Name' => $arr[2], 'MI' => $arr[3], 'Last_Name' =>
$arr[4], 'Firm' => $arr[5], 'Address' => $arr[6], 'type' => rtrim($arr[13]));

I need to take the different columns.. ID, Record_Type, etc. and update different tables with each value where the IDs match.

Importing A Flat Pipe Delimited File Into A MySQL Data Table
I have done this in perl but eeew I don't want to use perl anymore. I
want to be able to take a flat file that looks like this:

mbriones@...|Marian|Briones
bobsmith@...|Bob|Smith
janedoe@...|Jane|Doe
(etc)

The table has more than 3 fields, so I'd want to process the flat file
and then do an INSERT into mailinglist SET field=$value for each line.

Does someone have a nice way for me to do this? The flat file will be
uploaded to the server via a form (copy) and then be processed.

Exploding Tab Delimited Line
I'm trying to create an array from a tab delimited line of text. I've tried explode('/t',$line); but doesn't seem to work. What am I doing wrong?

Preg_match And Delimited Strings
I have a string I'd like to have broken into parts using preg_match. I
used a regular expression from the Perl FAQ (http://perlfaq.cpan.org/):

push(@new, $+) while $text =~ m{
"([^"]*(?:.[^"]*)*)",? # groups the phrase inside the
quotes
| ([^,]+),?
| ,
}gx;

The idea is that it splits delimited strings respecting the quotes, for
example...

foo, bar, "foo, bar", bar
would end up as
-foo
-bar
-"foo, bar"
-bar

So obviously an explode wont work.

I cant figure out how to convert that piece of perl above into
preg_match. I've copied the string and escaped all the appropriate
charecters however it still wont divide the string show above
properly...

$str = "foo, bar, "foo, bar", bar";
$re = ""([^"\]*(?:\.[^"\]*)*)",?| ([^,]+),?| ,";
if (preg_match($re, $str, $res)) {
print_r($res);
}

I have other strings that need parsing too (including ones with a
double encapsulator inside the string - eg. 'don''t touch that!'). But
I'm saving those for later.

Create A Delimited Text Array
How can I create a delimited text file to work with this script. I would like to put this first part in a text file and load it into an array so the php script can parse it. Code:

Is There A Php Command To Take A Specific Field From A Delimited Line Of Text?
I have a string which is multiple fields delimited by commas.

I would like a php command that says... take the 3rd field for
example.

ie. If MYVARIABLE has the data "hello,there,big,world".

I would like to be able to say, pickup the 3rd field "big" without
knowing the character positions or lengths.

Comma Seperator
I have numbers retrieved from database which is quite big. Is it possible to give a comma seperator in the sql query or otherwise
how can I format with a thousand comma separator?

Deleting Last Comma
The code below displays pet names like so:

Fido, Bowser, Kittycat, Spot,

and I want to delete only the last comma. I tried 'substr' and 'rtrim' and both of those delete the space and comma between each name, not just the last name as I'd like, so I end up with:
Fido,Bowser,Kittycat,Spot,

How would I delete only the last comma, so that it is this instead?
Fido, Bowser, Kittycat, Spot

Even with trim and substr, I end up with:

Comma Separated Values
What i've done is when the user enters the client list separated by commas..... this works, but I need to know how to warn the user when they have enter the client list with no commas.

Is there some sort of function which does this or is it a regular expression thing??

Seperating An Array With A Comma
I'm trying to seperate the output of an array with a comma. I can't figure out how to eliminate the last comma. Here is my code:

$result = mysql_query($query,$connection) or die (mysql_error());
$fw = fopen("/var/www/html/ecast/windex/graph.html","w");
fwrite($fw, "/ ");
if ($new_rows = mysql_fetch_array($result)){
do{
fwrite($fw, $new_rows[0]);
fwrite($fw, ", ");
}while($new_rows = mysql_fetch_array($result));
}
fwrite($fw, " /");
fclose($fw);
?>
Output: / 85, 86, 87, 89, /
How can I eliminate that last comma?

Convert Number To One With A Comma
How can I convert "2000" to "2,000?"

Removing A Comma From The End Of An Array
I am reading a file into an array using this:

$listFile = "add2db.txt";
if (!($fp = fopen($listFile, "r")))
exit("Unable to open the input file, $listFile.");
$buffer = fread($fp, filesize($listFile));
fclose($fp);

The very last character in that $listFile will always be a comma. What I need to do is remove that last comma (and only that comma) from $buffer before I can continue on. What do I need to do to accomplish this?

How To Instert Comma Into Number?
I am having users submit large numbers into a form, and when those values are returned to them later I want to have the commas in the correct places. ie. 123456.00 changes to 123,456.00. Is there a function that does this conversion? Also, how would I ensure that the number is always displayed with 2 decimal places, ie. 123 changes to 123.00, but 123.00 remains 123.00.

Strip Comma From Number
i have a number 1,553.00 i need to remove the comma and store the value? any ideas?

Re Comma Separated List - Another Question
I can do the match perfectly but what i also need to do is create a third
list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck ....

Breaking Up A Comma Seperated List
i have a list seperated by commas: 1. 3. 5. 7. 9 actually is a varible

$members ="1, 3, 5, 7, 9"; what would be the best way to break that up to insert the values into a table in a loop.

Comma Seperated List Comparison
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match?

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them

Exploding Comma Separated In Array
I'm looking to explode the comma separated values in an array like this:

Array ( => 10811 [1] => 7527,7556 )

and then add the exploded value as a new key in the array...like this:

Array ( => 10811 [1] => 7527 [2] => 7556 )

Seperator Instead Of A Comma For Listed Items In DB?
I'm making a recipe database, and need to have DB fields in mySQL that
will have lists of values that would get sent to an array to be worked
on.

I anticipate that at times a comma will need to be used in the value
itself. So, what's a commonly used symbol or something that people tend
to use as a separator of a list that would then be used in the split()
function to send the elements into an array?

How To Check If It's The Last Result And Remove Comma
I am wondering if anyone could help. I have a loop that displays my results and places a comma after each. The problem is that when the last result is displayed, it shows the comma.

Example:
for loop{
echo $row['test'].', ';

}

displays...  test1, test2, test3,

Notice after test3, there is still a comma? How do I remove it and have it display... test1, test2, test3

Comma Separated Text Files
A client has requested that I make a php app which takes an uploaded image, reads the values and enters them into a database. I've made the upload script which works fine, and the script which enters the values into the database, the only thing left now is how do I make PHP read the file and extract the values? The file is a text file, each field comma separated.

How To Remove Last Comma From A Multiple List?
I have a multiple select list in my site where the values are inserted in a database table, in a column named available_country. I enter these values in the db separated with comma. I don't know however how to remove the last comma so it will be something like this:

USA, Europe, Asia

and not:

USA, Europe, Asia,

Here's what I tried but it didn't worked: ...

Looping Thru Array And Creating Comma Sep String
I'm trying to loop through the elements of an array and create a comma separated string of the values, and then trim off the last comma. Here's the code I have which only displays the last element of the array in the string: PHP Code:

Comma Separated List In MySQL Field
say I have a list of categories in a field, and a page that is supposed to pull from the db based on a category. Is there any way I can do a query where it searches the contents of that field for the category in question?

like SELECT FROM table WHERE category = $category  but have it search the category field (the comma separated list for each entry) for the $category var

Seperate Links From Comma Seperated Value In A Databse
I have a value which is $keywords in my database The key words are separated by commas, like:  fun, funny, exciting, weird I want to make each of those keywords link to "link.php" I can only get them all to be one link, how would I make them each into seperate links.

How To Remove "comma, Full Stop" From A Sentence
I have sentences as follow:

$quote[1] = "To be or not to be, that is the question.";
$quote[2] = "Love looks not with the eyes, but with the mind.";
$quote[3] = "Love all, trust a few. Do wrong to none.";

now I would like to do the following,

1) for each quote[$i], remove all ","(comma) or "."(full stop)...does anyone know how? I have the following code but it does not work. What should I write in $patterns?

for ($i=1; $i<4; $i++)
{
$quote[$i] = strtolower($quote[$i]);
echo "$quote[$i]<BR>";
$pieces = explode(" ", $quote[$i]);
for ($j=0; $j< (count($pieces)); $j++)
{
echo "$pieces[$j] ";
$patterns = ".";
$replacement = "";
print preg_replace($patterns, $replacement, $string);
echo "$pieces[$j] <BR>";
}
}

2) also, I would like to count a) number of frequecy of each term (e.g. "to" has a frequency of 2 in qoute[1]) and b) number of frequency for all three quotes (i.e. "to" has frequency = 3)

but I have no idea how? can anyone please give me an idea?

Search String Delimited In Another String
I have a string (Website referrer).

Example: http://www.google.com/search?q=MY STRING HERE&hl=tl&client=firefox-a&channel=s&rls=org.mozilla:en-US:official&hs=dXZ&start=30&sa=N

I need to extract the keywords searched (example in the above case: "MY STRING HERE").

I think I need the string between "q=" and the next appearing "&".

How can I do this?

To Get A File Name, File Size, File Type...?
Is there a way to get information about a file BEFORE it is submitted to the server?
I am trying to make a upload progress bar from scratch WITHOUT THE PRON.C PHP FIX!!!
I found the file being uploaded in my NET://WINN01910//TEMP folder and I can outputs its file size in a popup with a one second refrehs, but this makes it so the USER needs to know the full file size to know if its anywehre near done. 

Is there a way to get the filesize from the clients side?  Possibly javascript that uses ajax to submit the values to the server before the file is sent but when it is selected?? Is there an easier way to do this and/or do you have any ajax example code of how it might work?

I also have another problem, Althoguh i can echo the filesize of what is recieved so far, I am just glob()ing the temp folder for: "php*.tmp" but is there a way to tell the tmp file so they don't see everyone's uploads?

FIle Uploads: Empty _POST And _FILES Arrays When File Too Large
Uploading of files.. AFAIU from the manual, if a file is larger than the
size defined in the form or larger than upload_max_filesize in php.ini,
that _FILES['file']['error'] should hold an INT error code.

The file upload system is working fine if the file is smaller than the
defined size, but if it's larger, then both the _POST and _FILES arrays
are completely empty:

array(0) {
}

I can obviously check to see if $_FILES is set and contains elements,
but I was hoping for more specific error handling (reason as to why the
upload failed).

Has anyone else encountered this? Any info welcomed =)

Checking File Existence Through A List Of File Locations Gathered From An Array
I've been trying to wrote a code to check the existence of some files gathered from a field in mySql database.

And I am kind a stuck at a point where I get the file locations from the database but when it passes those locations to file exist it keeps telling me the file does not exist.

Only first file exists the other 2 file does not exist to test if the code was working or not. Code:

Email With File Attachements - How To Upload File To Server From Browser?
I have a PHP script that sends an email with attachment and works great when
provided the path to the file to send.

However this file needs to be on the same server as the script.

I want to develop a webpage where people can send attachments that are
stored on their local PC.

I know I can use the <input type="file" > HTML to generate a file box that
allows the user to navigate to a file on their PC and product a file path,
but how do I then upload this file onto file server to email out with my PHP
email script?

Is there an easy way to do this?

Or can I approach this problem from a completely different angle.

Creating Link To File Or Temp File For User Download
I am designing a document management system (DMS). The files are stored
in a directory on the webserver. The filenames for the documents are
stored in a MySQL database. The DMS will track revisions from the
initial version through the updates. We must have strict permissions
set so only authorized personnel can access some documents. All of the
documents when uploaded are given totally random names such as
"114723fb5422c6bd5ed18f6.doc". Is there a way, without the webserver
creating a new file name "document_specs_ver2.doc" to have the file
download named "document_specs_ver2.doc".

If I must physically create a new file, how do I know when the
webserver would be allowed to delete it? Notes: The files will be in
several different file formats, not just "doc".

Header Function, File Download, Temp File Disappears
I have the following headers:

if (preg_match("/compatible; MSIE/i", "$browser_type")){
header("Content-Tranfer-Encoding: binary");
header("Content-type: ".$assoc["type"]."");
header("Content-Disposition: attachment; filename="".$assoc["name"].""");}
echo $assoc["file"];

which starts the download of an mp3 file in one particular case. If you do not save target as and just select 'open' the temp file is zero bytes even though the download process took a while and showd how much progress you were making. So something is happening to the temp file.

Warning: File(): URL File-access Is Disabled In The Server Configuration
Does any one know how to work around this error on a web site?

Warning: file(): URL file-access is disabled in the server configuration ...

Load .txt File From Computer And Save The File In Www/folder Director
anyone know how to add the .txt file from computer into the localhost www/Myfolder directory.

Download File And Delete File From Server With One Button Click
I want the user to be able to download a CSV file and delete the file from the server all with the click of a button.  Currently, in firefox, it will just display the CSV and not delete it. In IE, it will download the CSV and not delete it. Code:

Open File, Put First 5 Lines Into A Variable, Close File...
1) I want to open a file on my server.
2) I want to read only the first 5 lines.
3) Put the first five lines into a variable.
4) Close the file.
5) Display the contents of the variable.

I know how to use the fopen() but I have big problems just trying to read the first 5 lines only? I tried many things. I downloaded some search scripts which displayed the <meta> tag only from html pages...

What is this guy doing?

Well I have a big site and content is added and changed everyday. I have a dynamic page that reads a directory for files and then puts a link (well it includes the file for reading on my php page). That works great! MY PROBLEM is that I want not only a link but a small description below it (basically the first five lines on the file)...

See so far:
Quote: if (isset ($content)) {
include ("files/$content.html");
echo "<p><A HREF="read.php"><b>Back to index</b></A>";
} else {
echo "<b>Opening directory, reading files, and listing them as a link</b><p>";
$handle = opendir('./files');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo eregi_replace (".html", "", "<A HREF="read.php?content=$file">$file</a><p>");
}
}
closedir($handle);
}

Open .php File With PHP Designer 2007 By Clicking The File In XP
In windows XP, I want to be able to click on a .php file and have it
opened in PHP Designer 2007,
Or right-click on it and have an "edit" menu option presented to me.
I know how to tweak the registry to do this in general, but what would
the command line look like in this specific case ?
PHP Designer does not do this automatically. It is as if the program
does not allow commandline options.
It only opens with the last file used or a menu of recently opened
files.

PHP File Upload Unexpected Error - File Too Large?
I have a short basic script to upload files. It works fine with small files,
but with longer files it gets stuck.

Here's the fragment, the input file is loaded in "$file" from an HTML form
as usual.

....
$img_str = fread(fopen($file, "r"), filesize($file));
$data = addslashes($img_str);
// --------------------- up to here the execution is correct and fast (<1
second)

$sql="INSERT INTO $table (".
" file_name,".
" file_type,".
" file_size,".
" bin_data)".
" VALUES (".
" '$file_name',".
" '$file_type',".
" '$file_size',".
" '$data')";
// --------------------- it gets stuck
here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mysql_query($sql) or die ("SQL error ..$sql");

I have already set the php.ini variables as follows:
memory_limit = 16M
upload_max_filesize = 6M

I also checked the execution time to max_execution_time = 300 (even though I
don't believe it should be necessary).
The server is a P4 1.8 GHz, 256MB RAM


Copyright © 2005-08 www.BigResource.com, All rights reserved