Skip to main content

Posts

Showing posts from November, 2009

SimpleXML does not parse text inside CDATA tags in an XML.

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(simplexml_load_file too) actually takes 3 parameters.

* The string to parse
* Optional parameter – to return an object of class specified in this parameter. (By default it returns a SimpleXMLElement Object)

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
?>

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=&…