What does "Parse error: syntax error, unexpected ')'" mean? - php

Why am I getting the following error in PHP?
<?php
require_once('validation_functions.php');
$errors = array();
//$username = trim($_POST["username"]);
$username = trim("");
if(!has_presence($username))
{
$errors['username'] = "Username can't be left blank";
}
?>
<?php echo form_errors($errors); ?>
This is the following problem:
Parse error: syntax error, unexpected ')' in C:\wamp\www\sandbox\validation_functions.php on line 20
This is the file validation_functions.php detailed above
<?php
function has_presence($value)
{
//IF $value is SET AND value is NOT EXACTLY EQUAL TO "" RETURN TRUE
return (isset($value) && $value !== ""); //RETURNS BOOLEAN
}
function has_max_length($value, $max)
{
//IF $value is LESS THAN OR EQUAL TO $max Return TRUE
return (strlen($value) <= $max)); //RETURNS BOOLEAN
}
function has_inclusion_in($value, $set)
{
//IF $value is INCLUDED in $set RETURN TRUE
return in_array($value, $set); //RETURNS BOOLEAN
}
function form_errors($errors = array())
{
$output = "";
if(!empty($errors))
{
$output = "<div class= \"error\">";
$output .= "Please Fix The Following Errors:";
$output .= "<ul>";
foreach($errors as $key => $error)
{
$output .= "<li> {$error} </li>";
}
$output .= "</ul>";
$output .= "</div>";
}
return $output;
}
?>
This is included in the code referenced above. It's a simple error but I can't seem to understand what's causing it.

You have an extra bracket in the marked line
function has_max_length($value, $max)
{
//IF $value is LESS THAN OR EQUAL TO $max Return TRUE
>>>>>>>>> return (strlen($value) <= $max)); //<<<< here is a extra bracket, remove the last one!
Should be like this:
return (strlen($value) <= $max)

correct way: return (strlen($value) <= $max);
function has_max_length($value, $max)
{
//IF $value is LESS THAN OR EQUAL TO $max Return TRUE
return (strlen($value) <= $max); //RETURNS BOOLEAN
}

Related

preg_match - For sure there is a better way to search for these characters

So, I want to check the users-input, if it contains some of these characters:
" ' < >
I hope someone can show me a better way with less code
Thanks!
I used preg_match, but i just managed it with 4 nested if's.
/*Checks if the given value is valid*/
private function checkValidInput($input)
{
/*If there is no " */
if(preg_match('/"/', $input) == false)
{
/*If there is no ' */
if(preg_match("/'/", $input) == false)
{
/*If there is no <*/
if(preg_match("/</", $input) == false)
{
/*If there is no >*/
if(preg_match("/>/", $input) == false)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
You could create a regex class
preg_match('#["\'<>]#', $input);
Edit:
If you need to check for all characters then use strpos() with for loop
function checkInput($val) {
$contains = true;
$required = "<>a";
for($i = 0, $count = strlen($required); $i < $count ; ++$i) {
$contains = $contains && false !== strpos($val, $required[$i]);
}
return $contains;
}
var_dump(checkInput('abcd<>a')); // true
var_dump(checkInput('abcd>a')); // false, doesn't contain <

Getting Invalid argument supplied for foreach as a result of pass by reference variable

I am upgrading a codebase that makes use of pass by reference
Main function
function splitSqlFile(&$ret, $sql)
{
$sql = trim($sql);
$sql_len = strlen($sql);
$char = '';
$string_start = '';
$in_string = false;
for ($i = 0; $i < $sql_len; ++$i) {
$char = $sql[$i];
if ($in_string) {
for (;;) {
$i = strpos($sql, $string_start, $i);
if (!$i) {
$ret[] = $sql;
return true;
}else if ($string_start == '`' || $sql[$i-1] != '\\'){
......
}else {
......
} // end if...elseif...else
} // end for
}
else if ($char == ';') {
$ret[] = substr($sql, 0, $i);
$sql = ltrim(substr($sql, min($i + 1, $sql_len)));
$sql_len = strlen($sql);
if ($sql_len) {
$i = -1;
} else {
// The submited statement(s) end(s) here
return true;
}
}else if (($char == '"') || ($char == '\'') || ($char == '`')) {
$in_string = true;
$string_start = $char;
} // end else if (is start of string)
// for start of a comment (and remove this comment if found)...
else if ($char == '#' || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
......
if (!$end_of_comment) {
// no eol found after '#', add the parsed part to the returned
// array and exit
$ret[] = trim(substr($sql, 0, $i-1));
return true;
} else {
.....
} // end if...else
} // end else if (is comment)
} // end for
// add any rest to the returned array
if (!empty($sql) && trim($sql) != '') {
$ret[] = $sql;
}
return true;
}
Calling the function
$sqlUtility->splitSqlFile($pieces, $sql_query);
foreach ($pieces as $piece)
{
.......
}
If the above variable splitSqlFile(&$ret, $sql) have the "&" before it, the program does run successfully, but if it is removed, now splitSqlFile($ret, $sql), It will start returning the 'invalid argument supplied for foreach' error.and when I try using the "is_array" function to check if it is an array, the result is always "NULL".
Why you get the error:
By removing the & from $ret, you are no longer referencing the variable in the function call. In this case, $pieces. So when you do a foreach on $pieces after calling the function, it will error because $pieces is basically a null variable at that point.
function splitSqlFile(&$ret,$sql) {
$ret[] = 'stuff';
}
splitSqlFile($pieces,$sql);
// $pieces will be an array as 0 => 'stuff'
foreach ($pieces as $piece) { } // will not error
vs:
function splitSqlFile($ret,$sql) {
$ret[] = 'stuff';
}
splitSqlFile($pieces,$sql);
// $pieces will be a null variable, since it was never assigned anything
foreach ($pieces as $piece) { } // will error
Alternative to no reference:
So if you want to remove the & and no longer pass by reference, you have to do other changes to the function to get that value back out. And depending on the codebase, this could mean a whole lot of work everywhere that function is used!
Example:
function splitSqlFile($sql) {
$ret = [];
$ret[] = 'stuff';
return array('result'=>true,'ret'=>$ret);
}
// $result will contain multiple things to utilize
// if you will only need that variable once (does not accumulate)
$result = splitSqlFile($sql);
foreach ($result['pieces'] as $piece) { }
// if that variable is added by multiple calls, and displayed later... merge
$pieces = [];
$result = splitSqlFile($sql_1);
$pieces = array_merge($pieces,$result['pieces']);
$result = splitSqlFile($sql_2);
$pieces = array_merge($pieces,$result['pieces']);
foreach ($pieces as $piece) { }
A second example (passing in the array as you go... gets confusing):
function splitSqlFile($pieces_in,$sql) {
$pieces_in[] = 'stuff';
return array('result'=>true,'pieces_out'=>$pieces_in);
}
$pieces = [];
$result = splitSqlFile($pieces,$sql_1);
$pieces = $result['pieces_out'];
$result = splitSqlFile($pieces,$sql_2);
$pieces = $result['pieces_out'];
foreach ($pieces as $piece) { }
As you can see, not only does it change the return values that has to be dealt with, but it also changes how it is called. Again, if this function is used in a thousand places in the code... serious headaches!
Conclusion:
I would honestly keep the reference as it is. It was done that way to make accumulating debug data easier, and direct. Otherwise you have a lot of code changes to do toget rid of the reference.
However that can simply be my opinion on the matter.

How to repeat the same action for diffrent variables

<!-- language: php -->
<?php
// test variables
$l1 = "http://youtube.com/channel/";
$l2 = "http://youtube.com/channel/";
$l3 = "http://youtube.com/channel/";
$l4 = "http://youtube.com/channel/";
$fl = "http://youtube.com/channel/";
//set error false as default
$error = "false";
//check if variables are ready for use, if they are, add them to `$l` array
//I do each check as a seperate line, as it looks cleaner than 1 long if statement.
$l = [];
if(!empty($l1)) $l[] = $l1;
if(!empty($l2)) $l[] = $l2;
if(!empty($l3)) $l[] = $l3;
if(!empty($l4)) $l[] = $l4;
if(!empty($fl)) $l[] = $fl;
foreach($l as $key => $value) {
//1 line ternary is cleaner than if/else statetmnt
$errorKey = $key < 9? "0{$key}" : $key;
//each row by default has no error
$hasError = 0;
//check if this a valid url
if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $value)) {
$error = "true";
$hasError = 1;
}
if($hasError) {
//store error in array, to loop through later
$errors[] = $errorKey;
}
}
$search = '?sub_confirmation=1';
$searchUrl = "youtube.com/channel";
if (strpos($l, $searchUrl) !== false && strpos($l, $search) === false) {
$l = $value."".$search;
}
if($error == "false") {
echo $l1;
echo $l2;
echo $l3;
echo $l4;
echo $fl;
}
// deliver the error message
//Check if $error has been set to true at any point
if($error == "true") {
//loop through error array, echo error message if $errorNumber matches.
//at this point we KNOW there was an error at some point, no need to use a switch really
foreach($errors as $errorNumber) {
echo "Something went wrong here $errorNumber :o";
}
}
?>
Hello, my problem is at the end of the code where the strpos function is, so basically I want to check every url, once if it contains a certain url, and then add something to the end if it is so. But I don't want to repeat an if statement 4 times($fl variable doesn't has to be checked), I am quite new in all that so I hope somebody can help me, I tought about a switch statement but I guess there is a better way. And if I put it in the foreach aboth, it doesn't applies on the certain variables, only on the value variable.
You can assign $value by reference using this foreach header (notice the & in front of $value):
foreach($l as $key => &$value) {
By doing this every change you do to $value will also be done to the corresponding value in the $l array.
Then at the end of the foreach loop you put this code:
if (strpos($value, $searchUrl) !== false && strpos($value, $search) === false) {
$value .= $search;
}
So your final foreach loop should look like this:
foreach($l as $key => &$value) {
//1 line ternary is cleaner than if/else statetmnt
$errorKey = $key < 9? "0{$key}" : $key;
//each row by default has no error
$hasError = 0;
//check if this a valid url
if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $value)) {
$error = "true";
$hasError = 1;
}
if($hasError) {
//store error in array, to loop through later
$errors[] = $errorKey;
}
$search = '?sub_confirmation=1';
$searchUrl = "youtube.com/channel";
if (strpos($value, $searchUrl) !== false && strpos($value, $search) === false) {
$value .= $search;
}
}
You can read more about using references in foreach loops here: PHP: foreach
Edit:
To apply the changes not only to the elements of the $l array, but also to the original variables $l1, $l2 and so on, you should assign the elements to your array as references too:
$l = [];
if(!empty($l1)) $l[] = &$l1;
if(!empty($l2)) $l[] = &$l2;
if(!empty($l3)) $l[] = &$l3;
if(!empty($l4)) $l[] = &$l4;
if(!empty($fl)) $l[] = &$fl;
Personally, I think this is a good candidate for moving to a class. To be honest I'm not 100% sure what you are doing but will try to convert your code to a class.
class L {
public $raw = null;
public $modified = null;
public $error = false;
// create the class
public function __construct($data=null) {
$this->raw = $data;
// Check the raw passed in data
if ($data) {
$this->isUrl();
}
// If there was no error, check the data
if (! $this->error) {
$this->search();
}
}
// Do something ?
public function debug() {
echo '<pre>';
var_dump($this);
echo '</pre>';
}
public function getData() {
return ($this->modified) ? : $this->raw;
}
private function isUrl() {
$this->error = (! preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $this->raw));
}
// Should a failed search also be an error?
private function search() {
if ($this->raw) {
if ( (strpos($this->raw, "youtube.com/channel") !== false) &&
(strpos($this->raw, "?sub_confirmation=1") === false) ) {
$this->modified = $this->raw ."?sub_confirmation=1";
}
}
}
}
// Test data
$testList[] = "test fail";
$testList[] = "https://youtube.com/searchFail";
$testList[] = "https://youtube.com/channel/success";
$testList[] = "https://youtube.com/channel/confirmed?sub_confirmation=1";
// Testing code
foreach($testList as $key=>$val) {
$l[] = new L($val);
}
foreach($l as $key=>$val) {
// Check for an error
if ($val->error) {
$val->debug();
} else {
echo '<pre>'.$val->getData().'</pre>';
}
}
And the output would be:
object(L)#1 (3) {
["raw"]=>
string(9) "test fail"
["modified"]=>
NULL
["error"]=>
bool(true)
}
https://youtube.com/searchFail
https://youtube.com/channel/success?sub_confirmation=1
https://youtube.com/channel/confirmed?sub_confirmation=1

PHP syntax error in function, don't know why

I'm getting a syntax error and I can't figure out why. "Parse error: syntax error, unexpected '{' on line 6"
function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} else (is_array($customFields)) {
$parsed = $customFields;
}
// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}
return $returnField
}
You forget to put semicolon after $returnField, also use elseif instead of else (else don't need any arguments). Use the code below
function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} elseif (is_array($customFields)) {
$parsed = $customFields;
}
// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}
return $returnField;
}
Hope this helps you
else doesn't take any expression after it. Either remove the expression or use elseif instead.
You have missed semicolon in return
return $returnField;
}
Also Else condition else (is_array($customFields)) format is wrong....else dont take any condition after it...You can use Elseif instead
else (is_array($customFields)) {
Is invalid. Use either else if or remove the condition
function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == true && $customFields != null) {
$parsed = array($customFields);
} else {
$parsed = $customFields;
}
// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}
return $returnField;
}
It should else if(is_array($customFields)) or elseif(is_array($customFields))
I have modified your function, i see there is supposed to be and elseif instead of else and in the foreach loop, the syntax in your code was wrong. I have also assumed that you will be using a single level associative array.
<?php
function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} elseif (is_array($customFields)) {
$parsed = $customFields;
}
// loop through the fields and find the one we are looking for
$returnField = null;
foreach($customFields as $field) {
if ($field == $fieldName) {
$returnField = $field;
break;
}
}
return $returnField;
}
echo ExtractCustomField('name', array('name','emial','mobile','password'));
?>

PHP4 (legacy servers) XML=>JSON converion, a la simplexml_file_load() or simpleXMLelement

I'm developing on a server using an old version of PHP (4.3.9), and I'm trying to convert an XML string into a JSON string. This is easy in PHP5, but a lot tougher with PHP4.
I've tried:
Zend JSON.php
require_once 'Zend/Json.php';
echo Zend_Json::encode($sxml);
Error:
PHP Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}'
simplexml44
$impl = new IsterXmlSimpleXMLImpl;
$NDFDxmltemp = $impl->load_file($NDFDstring);
$NDFDxml = $NDFDxmltemp->asXML();
Error:
WARNING isterxmlexpatnonvalid->parse(): nothing to read
xml_parse
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "_start_element", "_end_element");
xml_set_character_data_handler($xml_parser, "_character_data");
xml_parse($xml_parser, $NDFDstring);
Error:
PHP Warning: xml_parse(): Unable to call handler _character_data() in ...
PHP Warning: xml_parse(): Unable to call handler _end_element() in ...
Does anyone have any other alternatives to simplexml_file_load() and new simpleXMLelement in PHP4?
Upgrading PHP is not an option in this particular case, so do not bother bringing it up. Yes, I know its old.
NOTE: This is the XML I'm trying to parse into a multidimensional array OR json.
http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?lat=40&lon=-120&product=time-series&begin=2013-10-30T00:00:00&end=2013-11-06T00:00:00&maxt=maxt&mint=mint&rh=rh&wx=wx&wspd=wspd&wdir=wdir&icons=icons&wgust=wgust&pop12=pop12&maxrh=maxrh&minrh=minrh&qpf=qpf&snow=snow&temp=temp&wwa=wwa
XML are quite easy to parse by yourself, anyway here is exists xml_parse in php 4 and domxml_open_file
and here is json for php4 and another one
according parsing xml:
if you know the structure of xml file, you can go even with RegExp, as XML is strict format (I mean all tags must be closed, all attributes in quotes, all special symbols always escaped)
if you parse arbitrary xml file, here is student sample, which works with php4, do not understand all XML features, but can give you "brute-force" like idea:
<?php
define("LWG_XML_ELEMENT_NULL", "0");
define("LWG_XML_ELEMENT_NODE", "1");
function EntitiesToString($str)
{
$s = $str;
$s = eregi_replace(""", "\"", $s);
$s = eregi_replace("<", "<", $s);
$s = eregi_replace(">", ">", $s);
$s = eregi_replace("&", "&", $s);
return $s;
}
class CLWG_dom_attribute
{
var $name;
var $value;
function CLWG_dom_attribute()
{
$name = "";
$value = "";
}
}
class CLWG_dom_node
{
var $m_Attributes;
var $m_Childs;
var $m_nAttributesCount;
var $m_nChildsCount;
var $type;
var $tagname;
var $content;
function CLWG_dom_node()
{
$this->m_Attributes = array();
$this->m_Childs = array();
$this->m_nAttributesCount = 0;
$this->m_nChildsCount = 0;
$this->type = LWG_XML_ELEMENT_NULL;
$this->tagname = "";
$this->content = "";
}
function get_attribute($attr_name)
{
//echo "<message>Get Attribute: ".$attr_name." ";
for ($i=0; $i<sizeof($this->m_Attributes); $i++)
if ($this->m_Attributes[$i]->name == $attr_name)
{
//echo $this->m_Attributes[$i]->value . "</message>\n";
return $this->m_Attributes[$i]->value;
}
//echo "[empty]</message>\n";
return "";
}
function get_content()
{
//echo "<message>Get Content: ".$this->content . "</message>\n";
return $this->content;
}
function attributes()
{
return $this->m_Attributes;
}
function child_nodes()
{
return $this->m_Childs;
}
function loadXML($str, &$i)
{
//echo "<debug>DEBUG: LoadXML (".$i.": ".$str[$i].")</debug>\n";
$str_len = strlen($str);
//echo "<debug>DEBUG: start searching for tag (".$i.": ".$str[$i].")</debug>\n";
while ( ($i<$str_len) && ($str[$i] != "<") )
$i++;
if ($i == $str_len) return FALSE;
$i++;
while ( ($i<strlen($str)) && ($str[$i] != " ") && ($str[$i] != "/") && ($str[$i] != ">") )
$this->tagname .= $str[$i++];
//echo "<debug>DEBUG: Tag: " . $this->tagname . "</debug>\n";
if ($i == $str_len) return FALSE;
switch ($str[$i])
{
case " ": // attributes comming
{
//echo "<debug>DEBUG: Tag: start searching attributes</debug>\n";
$i++;
$cnt = sizeof($this->m_Attributes);
while ( ($i<strlen($str)) && ($str[$i] != "/") && ($str[$i] != ">") )
{
$this->m_Attributes[$cnt] = new CLWG_dom_attribute;
while ( ($i<strlen($str)) && ($str[$i] != "=") )
$this->m_Attributes[$cnt]->name .= $str[$i++];
if ($i == $str_len) return FALSE;
$i++;
while ( ($i<strlen($str)) && ($str[$i] != "\"") )
$i++;
if ($i == $str_len) return FALSE;
$i++;
while ( ($i<strlen($str)) && ($str[$i] != "\"") )
$this->m_Attributes[$cnt]->value .= $str[$i++];
$this->m_Attributes[$cnt]->value = EntitiesToString($this->m_Attributes[$cnt]->value);
//echo "<debug>DEBUG: Tag: Attribute: '".$this->m_Attributes[$cnt]->name."' = '".$this->m_Attributes[$cnt]->value."'</debug>\n";
if ($i == $str_len) return FALSE;
$i++;
if ($i == $str_len) return FALSE;
while ( ($i<strlen($str)) && ($str[$i] == " ") )
$i++;
$cnt++;
}
if ($i == $str_len) return FALSE;
switch ($str[$i])
{
case "/":
{
//echo "<debug>DEBUG: self closing tag with attributes (".$this->tagname.")</debug>\n";
$i++;
if ($i == $str_len) return FALSE;
if ($str[$i] != ">") return FALSE;
$i++;
return TRUE;
break;
}
case ">";
{
//echo "<debug>DEBUG: end of attributes (".$this->tagname.")</debug>\n";
$i++;
break;
}
}
break;
}
case "/": // self closing tag
{
//echo "<debug>DEBUG: self closing tag (".$this->tagname.")</debug>\n";
$i++;
if ($i == $str_len) return FALSE;
if ($str[$i] != ">") return FALSE;
$i++;
return TRUE;
break;
}
case ">": // end of begin of node
{
//echo "<debug>DEBUG: end of begin of node</debug>\n";
$i++;
break;
}
}
if ($i == $str_len) return FALSE;
$b = 1;
while ( ($i<$str_len) && ($b) )
{
//echo "<debug>DEBUG: searching for content</debug>\n";
while ( ($i<strlen($str)) && ($str[$i] != "<") )
$this->content .= $str[$i++];
//echo "<debug>DEBUG: content: ".$this->content."</debug>\n";
if ($i == $str_len) return FALSE;
$i++;
if ($i == $str_len) return FALSE;
if ($str[$i] != "/") // new child
{
$cnt = sizeof($this->m_Childs);
//echo "<debug>DEBUG: Create new child (" . $cnt . ")</debug>\n";
$this->m_Childs[$cnt] = new CLWG_dom_node;
$this->m_Childs[$cnt]->type = LWG_XML_ELEMENT_NODE;
$i--;
if ($this->m_Childs[$cnt]->loadXML($str, $i) === FALSE)
return FALSE;
}
else
$b = 0;
}
$i++;
$close_tag = "";
while ( ($i<strlen($str)) && ($str[$i] != ">") )
$close_tag .= $str[$i++];
//echo "<debug>DEBUG: close tag: ".$close_tag." - ".$this->tagname."</debug>\n";
if ($i == $str_len) return FALSE;
$i++;
$this->content = EntitiesToString($this->content);
//echo "<debug>DEBUG: content: ".$this->content."</debug>\n";
return ($close_tag == $this->tagname);
}
}
class CLWG_dom_xml
{
var $m_Root;
function CLWG_dom_xml()
{
$this->m_Root = 0;
}
function document_element()
{
return $this->m_Root;
}
function loadXML($xml_string)
{
// check xml tag
if (eregi("<\\?xml", $xml_string))
{
// check xml version
$xml_version = array();
if ( (eregi("<\\?xml version=\"([0-9\\.]+)\".*\\?>", $xml_string, $xml_version)) && ($xml_version[1] == 1.0) )
{
// initialize root
$this->m_Root = new CLWG_dom_node;
$i = 0;
return $this->m_Root->loadXML(eregi_replace("<\\?xml.*\\?>", "", $xml_string), $i);
}
else
{
echo "<error>Cannot find version attribute in xml tag</error>";
return FALSE;
}
}
else
{
echo "<error>Cannot find xml tag</error>";
return FALSE;
}
}
}
function lwg_domxml_open_mem($xml_string)
{
global $lwg_xml;
$lwg_xml = new CLWG_dom_xml;
if ($lwg_xml->loadXML($xml_string))
return $lwg_xml;
else
return 0;
}
?>
PHP4 has no support for ArrayAccess nor does it have the needed __get(), __set() and __toString() magic methods which effectively prevents you from creating an object mimicking that feature of SimpleXMLElement.
Also I'd say creating an in-memory structure yourself which is done by Simplexml is not going to work out well with PHP 4 because of it's limitation of the OOP-object-model and garbage collection. Especially as I would prefer something like the Flyweight pattern here.
Which brings me to the point that you're probably more looking for an event or pull-based XML parser.
Take a look at the XML Parser Functions which are the PHP 4 way to parse XML with PHP. You find it well explained in years old PHP training materials also with examples and what not.

Categories