Using PHP to parse code from between tags
Posted: Sun Jan 22, 2012 12:24 am
Hi all,
I have a podcast xml file which I want to get tag values out of to put in a MySQL database. I've got a working php form which will parse some tags. However, not all. When I tell it to parse the text between <link> and </link> it returns blank fields instead of the actual link to the mp3 file. (example: http://www.recastweb.com/media/Luke/Luke01.mp3)
Also, I've notieced that if I don't include the itunes part of itunes tags (example: explicit instead of itunes:explicit) it works. When I give it the whole tag name <itunes:explicit> it parses nothing. And strangely, it doesn't see the caps in <pubDate> but it will return data when I use <pubdate>.
Do I have to escape the non-alpha characters to get this to work?
Here is the script I am sending the tag info to:
***** PLEASE USE THE PHP CODE TAG *****
I have a podcast xml file which I want to get tag values out of to put in a MySQL database. I've got a working php form which will parse some tags. However, not all. When I tell it to parse the text between <link> and </link> it returns blank fields instead of the actual link to the mp3 file. (example: http://www.recastweb.com/media/Luke/Luke01.mp3)
Also, I've notieced that if I don't include the itunes part of itunes tags (example: explicit instead of itunes:explicit) it works. When I give it the whole tag name <itunes:explicit> it parses nothing. And strangely, it doesn't see the caps in <pubDate> but it will return data when I use <pubdate>.
Do I have to escape the non-alpha characters to get this to work?
Here is the script I am sending the tag info to:
***** PLEASE USE THE PHP CODE TAG *****
Code: Select all
<html>
<head>
<title>Tag Parser</title>
<form name="configform" action="parser.php?action=saveconfig" method="POST">
<fieldset style="width: 650px;">
<legend>Results</legend>
<?php
$con = mysql_connect("xxxxxxxx","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("podadmin", $con);
function getTextBetweenTags($tag, $html, $strict=1)
{
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
if($strict==1)
{
$dom->loadXML($html);
}
else
{
$dom->loadHTML($html);
}
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
/*** the array to return ***/
$out = array();
foreach ($content as $item)
{
/*** add node value to the out array ***/
$out[] = $item->nodeValue;
}
/*** return the results ***/
return $out;
}
$tagtoparse = $_POST['title'];
$xhtml = '<item>
<title>Habakkuk 3</title>
<description>Pastor Todd Spitzer teaches from Habakkuk 3</description>
<link>http://www.recastweb.com/media/Habakkuk/Habakkuk_03.mp3</link>
<enclosure url="http://www.recastweb.com/media/Habakkuk/Habakkuk_03.mp3" length="46" type="audio/mpeg" ></enclosure>
<guid isPermaLink="false">EDB0E7B1-24BE-11DD-98A0-000A9592B578-250-0000004D07B366CF-FFA</guid>
<pubDate>Sun, 18 May 2008 13:01:35 +0300</pubDate>
<itunes:subtitle>Habakkuk 3</itunes:subtitle>
<itunes:summary>Pastor Todd Spitzer teaches from Habakkuk 3</itunes:summary>
<itunes:duration>45:54</itunes:duration>
<itunes:keywords>Regeneration, Todd Spitzer, Bible, Habakkuk</itunes:keywords>
<itunes:author>Todd Spitzer @ Regeneration</itunes:author>
<itunes:explicit>no</itunes:explicit>
</item>
<item>
<title>Habakkuk 2:4</title>
<description>Pastor Todd Spitzer teaches from Habakkuk 2:4</description>
<link>http://www.recastweb.com/media/Habakkuk/Habakkuk_02_04.mp3</link>
<enclosure url="http://www.recastweb.com/media/Habakkuk/Habakkuk_02_04.mp3" length="49" type="audio/mpeg" ></enclosure>
<guid isPermaLink="false">BDF78E36-24BE-11DD-98A0-000A9592B578-250-0000004C68A32FC6-FFA</guid>
<pubDate>Sun, 18 May 2008 13:01:29 +0300</pubDate>
<itunes:subtitle>Habakkuk 2:4</itunes:subtitle>
<itunes:summary>Pastor Todd Spitzer teaches from Habakkuk 2:4</itunes:summary>
<itunes:duration>48:54</itunes:duration>
<itunes:keywords>Regeneration, Todd Spitzer, Bible, Habakkuk</itunes:keywords>
<itunes:author>Todd Spitzer @ Regeneration</itunes:author>
<itunes:explicit>no</itunes:explicit>
</item>'
;
$content2 = getTextBetweenTags($tagtoparse, $xhtml, 5);
foreach ($content2 as $item) {
echo "$item<br />";
$sql="INSERT INTO test (test)
VALUES ('$item')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
mysql_close($con)
?>
</html>