Category Archives: php

Add attributes and children to a SimpleXML element

include ‘example.php’;

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute(‘type’, ‘documentary’);

$movie = $sxe->addChild(‘movie’);
$movie->addChild(‘title’, ‘PHP2: More Parser Stories’);
$movie->addChild(‘plot’, ‘This is all about the people who make it work.’);

$characters = $movie->addChild(‘characters’);
$character = $characters->addChild(‘character’);
$character->addChild(‘name’, ‘Mr. Parser’);
$character->addChild(‘actor’, ‘John Doe’);

$rating = $movie->addChild(‘rating’, ’5′);
$rating->addAttribute(‘type’, ‘stars’);

echo $sxe->asXML();


file_put_contents and file_get_contents replacement code


if (!function_exists(‘file_put_contents’)) {
function file_put_contents($filename, $data) {
$f = @fopen($filename, ‘w’);
if (!$f) {
return false;
} else {
$bytes = fwrite($f, $data);
return $bytes;

function get_file_contents($filename)

/* Returns the contents of file name passed

if (!function_exists(‘file_get_contents’))
$fhandle = fopen($filename, "r");
$fcontents = fread($fhandle, filesize($filename));
$fcontents = file_get_contents($filename);
return $fcontents;

function openFileSearchAndReplace($parentDirectory, $searchFor, $replaceWith)
//echo "debug here- line 1a\n";
//echo "$parentDirectory\n";
//echo "$searchFor\n";
//echo "$replaceWith\n";

if ($handle = opendir("$parentDirectory")) {
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..") && !is_dir($file)) {
chdir("$parentDirectory"); //to make sure you are always in right directory
// echo "$file\n";
$holdcontents = file_get_contents($file);
$holdcontents2 = str_replace($searchFor, $replaceWith, $holdcontents);
file_put_contents($file, $holdcontents2);
// echo "debug here- line 1\n";
// echo "$file\n";

if(is_dir($file) && ($file != "." && $file != ".."))
$holdpwd = getcwd();
//echo "holdpwd = $holdpwd \n";
$newdir = "$holdpwd"."/$file";
//echo "newdir = $newdir \n"; //for recursive call
openFileSearchAndReplace($newdir, $searchFor, $replaceWith);
//echo "debug here- line 2\n";
//echo "$file\n";

$parentDirectory2 = $argv[1];
$searchFor2 = $argv[2];
$replaceWith2 = $argv[3];

//Please do not edit below to keep the rights to this script
//Free license, if contents below this line is not edited
echo "REPLACED\n’$searchFor2′ with ‘$replaceWith2′ recursively through directory listed below\nFor all files that current user has write permissions for\nDIRECTORY: ‘$parentDirectory2′\n";
echo "command written by Kolapo Akande :) all rights reserved :)\n";

$holdpwd = getcwd();
//echo "$holdpwd\n";
openFileSearchAndReplace($parentDirectory2, $searchFor2, $replaceWith2);

Converting integet into month name

function monthName($month_int) {

$month_int = (int)$month_int;

$timestamp = mktime(0, 0, 0, $month_int, 1);

return date("F", $timestamp);


function js_monthName(month_int) {

var monthname = new

Array("", "January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December");



Regex quick reference

Regex quick reference

[abc] A single character: a, b or c
[^abc] Any single character but a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary character
(…) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{…} substitutions only once

php: catch ajax request

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
	getconnect($Ware_host, $Ware_user, $Ware_pswd, $Ware_database);
	$temp_detail = '';
	$temp_type = '';
		$temp_detail = addslashes($_POST['detail']);
		$temp_type = $_POST['type'];
	if($temp_detail && $temp_type)
		mysql_query("INSERT INTO $hidden_expenses_details (detail,type) VALUES ('$temp_detail','$temp_type') ");