chump2877 05-01-2006, 12:11 AM I'm getting a parse error for this line of code:
if (strpos($file_line, '<?xml') !== false)
And I can;t figure out why...I tried changing the single quotes to double quotes (for every strpos() call), but that just gave me a parse error for this line:
?>
The relevent code is as follows:
<?
// Start of functions
function readThisFile($xml_file)
{
if(!file_exists($xml_file))
{
die("The data file does not exist.");
}
chmod($xml_file, 0777);
if(!$handle = fopen($xml_file,"r"))
{
die("There was a problem opening the data file.");
}
while(!feof($handle))
{
$file_line = fgets($handle, 4096);
if (strpos($file_line, '<?xml') !== false)
{
continue;
}
elseif (strpos($file_line, '<!DOCTYPE') !== false)
{
continue;
}
elseif (strpos($file_line, '<reference>') !== false)
{
continue;
}
elseif (strpos($file_line, '</contact>') !== false)
{
continue;
}
elseif (strpos($file_line, '</reference>') !== false)
{
continue;
}
$content .= $file_line;
}
fclose($handle);
return $content;
}
function getElementInfo($element_name,$data_text)
{
if (empty($data_text))
{
return false;
}
$pattern = "/<" . $element_name . ">.*<\/" . $element_name . ">/";
preg_match($pattern, $data_text, $matches);
$pattern2 = "<" . $element_name . ">";
$matches[0] = str_replace($pattern2, "", $matches[0]);
$pattern3 = "</" . $element_name . ">";
$matches[0] = str_replace($pattern3, "", $matches[0]);
return trim($matches[0]);
}
?>
Plus, I;ve already used this code once before, and had no problems.
Anyone see what the problem is here? thanks.
ralph l mayo 05-01-2006, 01:29 AM You forgot to include the actual error. It all works fine for me, but I don't have shorttags enabled and that might be part of the issue. Try separating the tag:
if (strpos($file_line, '<' . '?xml') !== false)
missing-score 05-01-2006, 01:35 AM To my knowledge, tags should always be able to be printed in a string regardless of what tags are enabled. Seeing the error would help.
chump2877 05-01-2006, 01:51 AM it's just a plain parse error, nothing special or helpful in there:
Parse error: parse error in /var/www/html/blah/blah/modify_contact.php on line 107
missing-score 05-01-2006, 01:55 AM did you try the idea ralph l mayo suggested? to my knowledge it wont help becuase strings should be treated ok, but its worth a go as I cant see anything wrong with your code.
chump2877 05-01-2006, 01:58 AM You forgot to include the actual error. It all works fine for me, but I don't have shorttags enabled and that might be part of the issue. Try separating the tag:
if (strpos($file_line, '<' . '?xml') !== false)
Tried this, and it doesn;t help.
chump2877 05-01-2006, 02:00 AM did you try the idea ralph l mayo suggested? to my knowledge it wont help becuase strings should be treated ok, but its worth a go as I cant see anything wrong with your code.
just tried and it was a no-go...;)
There is some PHP, HTML and javascript that comes before these functions, could they somehow be causing the error....but I can;t see how really...I can post it here if you think it will help
bustamelon 05-01-2006, 03:22 AM I can post it here if you think it will help
It might. That line all by itself does not complain on my server, when I replace $file_line with a short text string.
I'm thinking it might have to do with parsing the actual file handle - maybe something with character set. Can we see a sample of the XML file too? Does it happen consistently with different source xml files?
chump2877 05-01-2006, 05:00 AM sample of xml file:
<?xml version="1.0"?>
<!DOCTYPE reference SYSTEM "contact_reference.dtd">
<reference>
<contact>
<firstname>blah</firstname>
<lastname>blah</lastname>
<business>blah</business>
<email>blah</email>
<website>blah</website>
<phone>blah</phone>
<address>
<street>blah</street>
<city>blah</city>
<state>blah</state>
<zip>blah</zip>
<country>blah</country>
</address>
</contact>
<contact>
<firstname>blah</firstname>
<lastname>blah</lastname>
<business>blah</business>
<email>blah</email>
<website>blah</website>
<phone>blah</phone>
<address>
<street>blah</street>
<city>blah</city>
<state>blah</state>
<zip>blah</zip>
<country>blah</country>
</address>
</contact>
</reference>
dtd file:
<?xml version="1.0"?>
<!ELEMENT reference (contact*)>
<!ELEMENT contact (firstname,lastname,business,email,website,phone,address)>
<!ELEMENT firstname (#CDATA)>
<!ELEMENT lastname (#CDATA)>
<!ELEMENT business (#CDATA)>
<!ELEMENT email (#CDATA)>
<!ELEMENT website (#CDATA)>
<!ELEMENT phone (#CDATA)>
<!ELEMENT address (street,city,state,zip,country)>
<!ELEMENT street (#CDATA)>
<!ELEMENT city (#CDATA)>
<!ELEMENT state (#CDATA)>
<!ELEMENT zip (#CDATA)>
<!ELEMENT country (#CDATA)>
And this is the only xml file i have available that is written for this dtd...
hope this helps..
Edit: also, please remember that I used the above PHP code in a different file to parse the same xml document, and IT WORKED.
It's just not working in this particular file....Here is the entire file:
<?
$c_name = $_GET['c_name'];
if (strpos($c_name, ',') !== false)
{
$c_name_array = explode(",", $c_name);
$ln = trim($c_name_array[0]);
$fn = trim($c_name_array[1]);
}
else
{
$fn = trim($c_name);
$ln = "";
}
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$xml_file = $_SERVER['DOCUMENT_ROOT'] . "/" . $uri . "/" . "contact_reference.xml";
$content2 = readThisFile($xml_file);
$contact_array2 = preg_split("/<contact>/", $content2);
unset($contact_array2[0]);
foreach ($contact_array2 as $key => $val)
{
$the_fn = getElementInfo("firstname",$val);
$the_ln = getElementInfo("lastname",$val);
if ($ln == $the_ln && $fn == $the_fn)
{
$b_name = getElementInfo("business",$val);
$the_email = getElementInfo("email",$val);
$the_website = getElementInfo("website",$val);'
$the_phone = getElementInfo("phone",$val);
$the_street = getElementInfo("street",$val);
$the_city = getElementInfo("city",$val);
$the_state = getElementInfo("state",$val);
$the_zip = getElementInfo("zip",$val);
$the_country = getElementInfo("country",$val);
break;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">
<head>
<title>Modify Contact</title>
<link rel="stylesheet" type="text/css" href="contact_reference.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<body>
<form action="contact_reference.php" method="post">
<p>First Name: <input type="text" name="first_name" size="25" value="<? echo $the_fn; ?>"></p>
<p>Last Name: <input type="text" name="last_name" size="25" value="<? echo $the_ln; ?>"></p>
<p>Business Name: <input type="text" name="business_name" size="25" value="<? echo $b_name; ?>"></p>
<p>E-mail Address: <input type="text" name="email" size="25" value="<? echo $the_email; ?>"></p>
<p>Website Address: <input type="text" name="website" size="25" value="<? echo $the_website; ?>"></p>
<p>Phone Number: <input type="text" name="phone" size="25" value="<? echo $the_phone; ?>"></p>
<p>Street Address: <input type="text" name="street" size="25" value="<? echo $the_street; ?>"></p>
<p>City: <input type="text" name="city" size="25" value="<? echo $the_city; ?>"></p>
<p>State/Region: <input type="text" name="state" size="20" value="<? echo $the_state; ?>"></p>
<p>Postal Code: <input type="text" name="zip" size="15" value="<? echo $the_zip; ?>"></p>
<p>Country: <input type="text" name="country" size="25" value="<? echo $the_country; ?>"></p>
<p style="text-align:center"><input type="submit" name="add_submit" value="Update Contact"><input type="reset" value="Reset Form"></p>
</form>
</body>
</html>
<?
// Start of functions
function readThisFile($xml_file)
{
if(!file_exists($xml_file))
{
die("The data file does not exist.");
}
chmod($xml_file, 0777);
if(!$handle = fopen($xml_file,"r"))
{
die("There was a problem opening the data file.");
}
while(!feof($handle))
{
$file_line = fgets($handle, 4096);
if (strpos($file_line, '<?xml') !== false)
{
continue;
}
elseif (strpos($file_line, '<!DOCTYPE') !== false)
{
continue;
}
elseif (strpos($file_line, '<reference>') !== false)
{
continue;
}
elseif (strpos($file_line, '</contact>') !== false)
{
continue;
}
elseif (strpos($file_line, '</reference>') !== false)
{
continue;
}
$content .= $file_line;
}
fclose($handle);
return $content;
}
function getElementInfo($element_name,$data_text)
{
if (empty($data_text))
{
return false;
}
$pattern = "/<" . $element_name . ">.*<\/" . $element_name . ">/";
preg_match($pattern, $data_text, $matches);
$pattern2 = "<" . $element_name . ">";
$matches[0] = str_replace($pattern2, "", $matches[0]);
$pattern3 = "</" . $element_name . ">";
$matches[0] = str_replace($pattern3, "", $matches[0]);
return trim($matches[0]);
}
?>
chump2877 05-01-2006, 05:34 AM LOL, I wasn;t using my color coded editor...now the error is plain as day:
$the_website = getElementInfo("website",$val);'
$the_phone = getElementInfo("phone",$val);
grrrr :rolleyes:
bustamelon 05-02-2006, 03:23 PM :thumbsup:
|
|