SimpleXML does not parse text inside CDATA tags in an XML.
Consider the XML below:
$str = '';
$str.='some text goes here';
$str.='';
To parse it we use following syntax:
$xml = simplexml_load_string($str);
On printing it outputs:
SimpleXMLElement Object
(
[childNode] => some text goes here
)
Thats OK. Now the same xml but this time the text is enclosed in CDATA tags.
$str = '';
$str.='';
$str.=''
On printing this gives following output:
SimpleXMLElement Object
(
[childNode] => SimpleXMLElement Object
(
)
)
Yes its empty. This is because SimpleXML does not parse CDATA tags. All data enclosed within CDATA is ignored by SimpleXML parser.
Solution: Set the 3rd parameter to LIBXML_NOCDATA while parsing.
simplexml_load_string(
* The string to parse
* Optional parameter – to return an object of class specified in this parameter. (By default it returns a SimpleXMLElement Object)
* Also optional – libxml parameters can be specified as options. This option provides the solution to our CDATA problem
Provide the 3rd parameter LIBXML_NOCDATA and SimpleXML will consider CDATA nodes as text nodes and will parse them.
$xml = simplexml_load_string($str,'
This will output as desired:
SimpleXMLElement Object
(
[childNode] => some text goes here
)
Please note that using the third parameter requires PHP >=5.1 compiled with libxml.
Secure file download in PHP, Security question PHP, PHP MYSQL Interview Question -Books download - PHP solutions guidelines queries update, phpmysqlquestion
Friday, November 27, 2009
SimpleXML does not parse text inside CDATA tags in an XML.
Saturday, November 21, 2009
Brent's web log: jQuery social bookmarking plugin
Brent's web log: jQuery social bookmarking plugin: "http://s7.addthis.com/static/btn/lg-share-en.gif"
Wednesday, November 11, 2009
Find how many number of result of your site on google
<?php
/* return result number */
function get_google_results($domain = 'vijayjoshi.org')
{
// get the result content
$content = file_get_contents('http://www.google.com/search?q=site:'.$domain);
// parse to get results
$result = get_match('/Results <b>(.*)from/isU',$content);
// echo $result;
// split the results
$split1 = explode('of about',$result);
// return result
return $split1[1] ? strip_tags($split1[1]) : 0;
}
/* helper: does the regex */
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}
/* do it! */
echo 'vijayjoshi.org: '.get_google_results('vijayjoshi.org'); // 164
//echo 'Philogy.com: '.get_google_results('philogy.com'); // 164
?>
/* return result number */
function get_google_results($domain = 'vijayjoshi.org')
{
// get the result content
$content = file_get_contents('http://www.google.com/search?q=site:'.$domain);
// parse to get results
$result = get_match('/Results <b>(.*)from/isU',$content);
// echo $result;
// split the results
$split1 = explode('of about',$result);
// return result
return $split1[1] ? strip_tags($split1[1]) : 0;
}
/* helper: does the regex */
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}
/* do it! */
echo 'vijayjoshi.org: '.get_google_results('vijayjoshi.org'); // 164
//echo 'Philogy.com: '.get_google_results('philogy.com'); // 164
?>
Tuesday, November 10, 2009
How to upload file with FTP in PHP
<?php
/*
***************************************************************************************
***************************************************************************************
*/
prepare a form similiar to this and have it call the below file
echo '<form action="image_upload.php" method="post" enctype="multipart/form-data">';
echo 'Click the Browse button to find the file you wish to upload';
echo '<input type="file" name="imagefile">';
echo '<INPUT TYPE="submit" name="upload" value="upload">';
echo '</form>';
/**************************************************************************************
***************************************************************************************
***************************************************************************************
*** <input type="file" name="imagefile"> ***
*** with the above tag declared in the calling form ***
*** the variable name is $imagefile and the available properties are ***
*** $imagefile :name of the file as stored on the temporary server directory ***
*** $imagefile_name :filename.extension of the file as on the users machine ***
*** $imagefile_size :size in bytes of the file ***
*** $imagefile_type :the type of file image/gif image/jpg text/html etc.... ***
*** ***
***************************************************************************************
***************************************************************************************
*/
//change these values to suit your site
$ftp_user_name='XXXXXXXX';
$ftp_user_pass='XXXXXXXX';
$ftp_server='ftp.YOURSITE.com';
$ftp_dir='/YOURSITE.COM/public_html/upload/';
//$web_location is needed for the file_exists function, the directories used by FTP
//are not visible to it will will always return not found.
$web_dir='../upload/';
$web_location=$web_dir.$imagefile_name;
//build a fully qualified (FTP) path name where the file will reside
$destination_file=$ftp_dir.$imagefile_name;
// connect, login, and transfer the file
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$upload = ftp_put($conn_id, $destination_file, $imagefile, FTP_BINARY);
//use ftp_site to change mode of the file
//this will allow it be visible by the world,
$ch=ftp_site($conn_id,"chmod 777 ".$destination_file);
// close the FTP stream
ftp_close($conn_id);
//verify file was written
if (file_exists($web_location))
{
echo "file was uploaded as $web_location";
}
else
{
echo "Could not create $web_location";
}
//end if
?>
/*
***************************************************************************************
***************************************************************************************
*/
prepare a form similiar to this and have it call the below file
echo '<form action="image_upload.php" method="post" enctype="multipart/form-data">';
echo 'Click the Browse button to find the file you wish to upload';
echo '<input type="file" name="imagefile">';
echo '<INPUT TYPE="submit" name="upload" value="upload">';
echo '</form>';
/**************************************************************************************
***************************************************************************************
***************************************************************************************
*** <input type="file" name="imagefile"> ***
*** with the above tag declared in the calling form ***
*** the variable name is $imagefile and the available properties are ***
*** $imagefile :name of the file as stored on the temporary server directory ***
*** $imagefile_name :filename.extension of the file as on the users machine ***
*** $imagefile_size :size in bytes of the file ***
*** $imagefile_type :the type of file image/gif image/jpg text/html etc.... ***
*** ***
***************************************************************************************
***************************************************************************************
*/
//change these values to suit your site
$ftp_user_name='XXXXXXXX';
$ftp_user_pass='XXXXXXXX';
$ftp_server='ftp.YOURSITE.com';
$ftp_dir='/YOURSITE.COM/public_html/upload/';
//$web_location is needed for the file_exists function, the directories used by FTP
//are not visible to it will will always return not found.
$web_dir='../upload/';
$web_location=$web_dir.$imagefile_name;
//build a fully qualified (FTP) path name where the file will reside
$destination_file=$ftp_dir.$imagefile_name;
// connect, login, and transfer the file
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$upload = ftp_put($conn_id, $destination_file, $imagefile, FTP_BINARY);
//use ftp_site to change mode of the file
//this will allow it be visible by the world,
$ch=ftp_site($conn_id,"chmod 777 ".$destination_file);
// close the FTP stream
ftp_close($conn_id);
//verify file was written
if (file_exists($web_location))
{
echo "file was uploaded as $web_location";
}
else
{
echo "Could not create $web_location";
}
//end if
?>
Subscribe to:
Posts (Atom)
How to solve mysql ERROR 1118 (42000) Row size too large
I had this issue with MYSQL 5.7 . The following worked althoug...
-
PHP has an SSH2 library which provides access to resources (shell, remote exec, tunneling, file transfer) on a remote ma...
-
Which pillar of the AWS Well-Architected Framework recommends maintaining infrastructure as code? Operational Excellence Which of the foll...
-
Introduction to PHP PDO (PHP Data Objects) 1. What is PDO 2. What Databases does PDO support 3. Where do I begin? 4. Connect to ...