unexpected[it]

Professional IT Blog

  • 18th, Aug 2010

PHP Handling Non-English Characters UTF8

Tags: , , ,

	/**
	* Returns an string with the same length.
	* - If it is smaller than num it will fill out with the fill character.
	* - If it is larger than num it will cut the string.
	* www.unexpectedit.com
	*/
	function formatcell($data, $num, $fill=' '){
	    $data = trim($data);
		$data=str_replace(chr(13),' ',$data);
		$data=str_replace(chr(10),' ',$data);
		// translate UTF8 to English characters
		$data = iconv('UTF-8', 'ASCII//TRANSLIT', $data);
		$data = preg_replace("/[\'\"\^\~\`]/i", '', $data);

		// fill it up with spaces
    	for ($i = strlen($data); $i < $num; $i++) {
    		$data .= $fill;
    	}
    	// limit string to num characters
   		$data = substr($data, 0, $num);

		return $data;
    }

    echo formatcell("YES UTF8 String Zürich", 25, 'x'); //YES UTF8 String Zürichxxx
    echo formatcell("NON UTF8 String Zurich", 25, 'x'); //NON UTF8 String Zurichxxx
  • 4th, Aug 2010

.htaccess PHP Error Reporting

Tags: , ,

php_flag display_errors off
php_value error_reporting 7
  • 22nd, Jul 2010

PHP mysqldump Script – Backup a MySQL Database

Tags: , , , , , ,

/**
* PHP mysqldump Script
* www.unexpectedit.com
*/
$dbname="magentoDatabase";
$dbhost="localhost";
$dbuser="root";
$dbpass="123456";

$backupFile = $dbname . date("Y-m-d-H-i-s")  . '.gz';
$command = "mysqldump --opt -h $dbhost -u'$dbuser' -p'$dbpass' $dbname | gzip > $backupFile";
$returned = system($command);
echo $returned;
  • 14th, Jun 2010

PHP Email Functions

Tags: , , , ,

/**
* PHP Email Functions
* www.unexpectedit.com
*/
function sendEmail($to, $from, $subject, $message, $files) {
	$headers = "From: $from";
	// boundary
	$semi_rand = md5(time());
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
	// headers for attachment
	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
	// multipart boundary
	$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
	$message .= "--{$mime_boundary}\n";
	// preparing attachments
	for($x=0;$x<count($files);$x++){
		$file = fopen($files[$x],"rb");
		$data = fread($file,filesize($files[$x]));
		fclose($file);
		$data = chunk_split(base64_encode($data));
		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
		$message .= "--{$mime_boundary}\n";
	}
	// send
	$ok = @mail($to, $subject, $message, $headers);
	return $ok;
}

//Usage:
// array with filenames to be sent as attachment
$files = array("file_1.ext","/tmp/file_2.ext","/home/guest/file_3.ext");
// email fields: to, from, subject, and so on
$to = "root@localhost";
$from = "mail@mail.com";
$subject ="My subject";
$message = "My message";

sendEmail($to, $from, $subject, $message, $files);
  • 14th, Jun 2010

PHP Browser Functions

Tags: , , ,

/**
* Browser Detection Functions
* www.unexpectedit.com
*/
function inAgent($agent) {
	global $_SERVER;
	return (strpos($_SERVER['HTTP_USER_AGENT'], $agent) === false) ? false : true;
}

function whichBrowser() {
	/*
	* $browser will contain one of the following values:
	* 'iewin' : IE 4+ for Windows
	* 'iemac' : IE 4 for Macintosh
	* 'ie5mac' : IE 5 Macintosh
	* 'nswin' : Netscape 4.x Windows
	* 'nsunix' : Netscape 4.x Unix
	* 'nsmac' : Netscape 4.x Mac
	* 'ns6' : Netscape 6 / Mozilla
	*/
	switch (inAgent('MSIE'))
	{
		case true:
			if ( inAgent('Mac') ) {
				$browser = inAgent('MSIE 5') ? 'ie5mac' : 'ie4mac';
			}
			elseif ( inAgent('Win') )
			{
				$browser = 'iewin';
			}
			break;

		case false:
			if (inAgent('Mozilla/5')) {
				$browser = 'ns6';
			}
			elseif (inAgent('Mozilla/4'))
			{
				if ( inAgent('Mac'))
				{
					$browser = 'nsmac';
				}
				else
				{
				$browser = (inAgent('Win')) ? 'nswin' : 'nsunix';
				}
			}
			else
			{
				$browser = "unknown";
			}
			break;
	}
	return($browser);
}

// browser detection - end
switch (whichBrowser())
{
	case 'iewin': case 'iemac': case 'ie5mac':
		echo "independent";
		break;
	case 'nswin': case 'nsunix': case 'nsmac': case 'ns6':
		echo "microsoft";
		break;
}
  • 11th, Jun 2010

PHP Security Functions

Tags: , , , , ,

/**
* Security Functions
* www.unexpectedit.com
*/
//UID generate unique random number/letters or a random password
function generateUID($length=32) {
	$pass="";
	for($k=0; $k < $length; $k++) {
		$prob = mt_rand(1,10);
		if($prob <= 3) // A-Z probability: 30%
			$pass .=  chr(mt_rand(65,90));
		if($prob <= 6 )   //a-z probability is 30%
			$pass .= chr(mt_rand(97,122));
		else //0-9 probability is 40%
			$pass .= chr(mt_rand(48, 57));
	}
	return $pass;
}
  • 10th, Jun 2010

PHP Send Mail Function

Tags: , ,

/**
* Send an email with file attached
* www.unexpectedit.com
*/
public function send($fileatt, $from, $to) {
 $headers = "From: "."ordermanagement@handpickedcollection.com";
 $semi_rand = md5(time());
 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
  $email_message = "This is a multi-part message in MIME format.\n\n" .
   "--{$mime_boundary}\n" .
   "Content-Type:text/plain; charset=\"iso-8859-1\"\n" .
   "Content-Transfer-Encoding: 7bit\n\n";
   /* PREPARE ATTACHMENT */
   $fileatt_type = "application/octet-stream";
   $file = fopen($fileatt,'rb');
   $data = fread($file,filesize($fileatt));
   fclose($file);
   $data = chunk_split(base64_encode($data));
   $email_message .= "--{$mime_boundary}\n" .
   "Content-Type: application/octet-stream;\n" .
   " name=\"{$fileatt}\"\n" .
   "Content-Transfer-Encoding: base64\n\n" .
   $data . "\n\n" .
   "--{$mime_boundary}--\n";
   $strto=gmdate("dS M,Y H:i", strtotime($to));
   $ok = mail("ignacio@richardmason.net", "Unprocessed orders:      $strto", $email_message, $headers);
   return($ok);
 }
  • 10th, Jun 2010

PHP Text Manipulation Functions

Tags: , , , , ,

/**
* Text Manipulation Functions
* www.unexpectedit.com
*/
function html_cut_text($text,$limit) {
	$max_code=8; // max for special code like &permil; &aacute; ...
	if(($limit<>0)&&(strlen($text)>$limit)) {
		$pos=strpos(substr($text,0,$limit+$max_code),';'); //find ;
	if($pos) // cut when pass ;
		$text=substr($text,0,$pos+1);
	else //cut at limit
		$text=substr($text,0,$limit);
	return($text."...");
	}
	else
		return($text); // return
}
  • 10th, Jun 2010

PHP Get Requested Page

Tags: , ,

$identifier=$_SERVER['REQUEST_URI'];
$page=substr($identifier, strrpos($identifier,'/') + 1, strlen($identifier));
//full url
"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//Note: It should implement port if the is a https page
  • 10th, Jun 2010

PHP Escape Sequences

Tags: , , , , ,

\n //line feed
\r //carriage return
\t //horizontal tab
\\ //backslash
\” //double quote
\nnn //character corresponding to the octal value of nnn (with each digit being between 0 and 7)
\xnn //character corresponding to the hexadecimal value of nn

© 2010 unexpected[it]. All Rights Reserved.

This blog is powered by Wordpress and a modified version of Magatheme