| | |
PHP and XML Question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I have this csv2xml.php file
And I get this results
How can I get rid of the empty tags?
Thank you
PHP Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); function csv2xml($file, $container = 'data') { $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $r .= "\t\t<{$titles[$i]}>"; $r .= $data[$i]; $r .= "</{$titles[$i]}>\n"; } $row++; } fclose($handle); $r .= "</{$container}>"; return $r; } $xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> '; $xml .= csv2xml('dtifeed.csv', 'petad'); $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?>
And I get this results
PHP Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="ISO-8859-1" ?> <petad> <ID>456493</ID> <Category>P105</Category> <></> <></> <Description>DOGS FOR SALE - They are the best dogs ever. This is a test ad, I would never sell my dogs. Test ad. Please call 928-555-1212. </Description> <ID>456501</ID> <Category>P105</Category> <></> <></> <Description>FIDO MUST GO - But not because we don't love him. Because this is a test ad. The best test ad you've ever read. $1,000. 928-555-1212 </Description> <ID>456503</ID> <Category>P110</Category> <></> <></> <Description>KITTENS FREE TO A GOOD HOME - All must go together! There are 101 of them. 928-555-6868.456504</Description> <ID>456514</ID> <Category>P125</Category> <></> <></> <Description>A HORSE IS A HORSE unless it's a test ad. Horses for sale, $8,000. 928-111-2323.456516</Description> </petad>
How can I get rid of the empty tags?
Thank you
this should do it.
php Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); function csv2xml($file, $container = 'data') { $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":""; } $row++; } fclose($handle); $r .= "</{$container}>"; return $r; } $xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> '; $xml .= csv2xml('dtifeed.csv', 'petad'); $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?>
Last edited by R0bb0b; Oct 1st, 2008 at 4:09 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
You can explode the description field with explode(" ", $description) and then loop through the array with a regular expression(just like you would with an email text field validation). In the case that you do get one or more that comes up true, add them to an emails array and after you are done with the description you can then loop through the emails array echoing them with <email> and </email> surrounding email email address.
make sense?
make sense?
Last edited by R0bb0b; Oct 2nd, 2008 at 3:03 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
•
•
You can explode the description field with explode(" ", $description) and then loop through the array with a regular expression(just like you would with an email text field validation). In the case that you do get one or more that comes up true, add them to an emails array and after you are done with the description you can then loop through the emails array echoing them with <email> and </email> surrounding email email address.
make sense?
I also made some adjustments to help keep it xml compliant
php Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); function csv2xml($file, $container = 'data') { $dataarray = array(); $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $emailsarray = array(); if(strtolower($titles[$i]) == "description") { foreach(explode(" ", $data[$i]) as $value) { if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value)) { $emailsarray[] = $value; } } } $dataarray[$titles[$i]][] = $data[$i]; foreach($emailsarray as $value) { $dataarray["email"][] = $value; } } $row++; } fclose($handle); $containers = array("email"); foreach($dataarray as $key=>$value) { if(in_array(strtolower($key), $containers)) { $r .= "\t\t<" . $key . "-container>\n"; } foreach($value as $value2) { $tabchars = "\t\t"; if(in_array(strtolower($key), $containers)) { $tabchars .= "\t\t"; } $r .= $tabchars . "<$key>$value2</$key>\n"; } if(in_array(strtolower($key), $containers)) { $r .= "\t\t</" . $key . "-container>\n"; } } $r .= "</{$container}>\n"; return $r; } $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $xml .= csv2xml('dtifeed.csv', 'petad'); echo "<xmp>" . $xml . "</xmp>"; $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?>
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Hello I added my code to your code so I can be able to write to the new xml file and also the trim function and is not working it gives me a blank page
PHP Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd if (!file_exists($file)) touch($file); $fh = fopen($file, "r"); function csv2xml($file, $container = 'data') { $dataarray = array(); $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":""; } $emailsarray = array(); if(strtolower($titles[$i]) == "description") { foreach(explode(" ", $data[$i]) as $value) { if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value)) { $emailsarray[] = $value; } } } $dataarray[$titles[$i]][] = $data[$i]; foreach($emailsarray as $value) { $dataarray["email"][] = $value; } } $row++; } fclose($handle); $containers = array("email"); foreach($dataarray as $key=>$value) { if(in_array(strtolower($key), $containers)) { $r .= "\t\t<" . $key . "-container>\n"; } foreach($value as $value2) { $tabchars = "\t\t"; if(in_array(strtolower($key), $containers)) { $tabchars .= "\t\t"; } $r .= $tabchars . "<$key>$value2</$key>\n"; } if(in_array(strtolower($key), $containers)) { $r .= "\t\t</" . $key . "-container>\n"; } } $r .= "</{$container}>\n"; return $r; } $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $xml .= csv2xml('petsfeed.csv', 'petad'); echo "<xmp>" . $xml . "</xmp>"; $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?>
I discover the problem this is
DO I need to name r different???
PHP Syntax (Toggle Plain Text)
$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
DO I need to name r different???
•
•
•
•
I discover the problem this is
PHP Syntax (Toggle Plain Text)
$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
DO I need to name r different???
php Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd if (!file_exists($file)) touch($file); $fh = fopen($file, "r"); function csv2xml($file, $container = 'data') { $dataarray = array(); $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $emailsarray = array(); if(strtolower($titles[$i]) == "description") { foreach(explode(" ", $data[$i]) as $value) { if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value)) { $emailsarray[] = $value; } } } $dataarray[$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:""; foreach($emailsarray as $value) { $dataarray["email"][] = $value; } } $row++; } fclose($handle); $containers = array("email"); foreach($dataarray as $key=>$value) { if(in_array(strtolower($key), $containers)) { $r .= "\t\t<" . $key . "-container>\n"; } foreach($value as $value2) { $tabchars = "\t\t"; if(in_array(strtolower($key), $containers)) { $tabchars .= "\t\t"; } $r .= $tabchars . "<$key>$value2</$key>\n"; } if(in_array(strtolower($key), $containers)) { $r .= "\t\t</" . $key . "-container>\n"; } } $r .= "</{$container}>\n"; return $r; } $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $xml .= csv2xml('petsfeed.csv', 'petad'); $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?>
it has been changed to this line
$dataarray[$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";
The reason I added the data to $dataarray is because it is possible now to have multiple email addresses and it is (I believe) xml compliant that when you have multiple of anything that they should go into a container. Or maybe it is just good practice.
This line here defines your containers:
$containers = array("email");
Another thing that this does is if you have an email field and you are also pulling email addresses out of the description, you want them to be grouped together, the $dataarray allows you to do that by adding all of the email address to $dataarray['email'] key and then looping through after all the data is looped through.
Last edited by R0bb0b; Oct 2nd, 2008 at 6:09 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Similar Threads
- How to call a PHP function from Javascript and return the results back into Javascrip (PHP)
- Question re: education (IT Professionals' Lounge)
- Multi-Language Interface in PHP/XML or PHP/MySQL (PHP)
- How can insert PHP snip code into a php website (PHP)
- Xml? (IT Professionals' Lounge)
- Inserting Elements into existing XML (C#)
- using php to check for filenames (PHP)
- How to use xml? (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: Symfony MOdule redirection
- Next Thread: Multiple SQL Queries in PHP page
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend





