Tags: same length, utf8, utf8_decode, utf8_encode
/**
* 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
Tags: display_errors, error_reporting, E_ALL
php_flag display_errors off
php_value error_reporting 7
Tags: backup, database, export, mysql, mysqldump, PHP, script
/**
* 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;
Tags: attach, email, files, function, PHP
/**
* 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);
Tags: browser detection, HTTP_USER_AGENT, PHP, S_SEVER["HTTP_USER_AGENT"]
/**
* 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;
}
Tags: generate, mt_rand, PHP, random, security, uid
/**
* 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;
}
Tags: attachment, email, PHP
/**
* 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);
}
Tags: functions, manipulation, PHP, string, text, uid
/**
* Text Manipulation Functions
* www.unexpectedit.com
*/
function html_cut_text($text,$limit) {
$max_code=8; // max for special code like ‰ á ...
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
}
Tags: page, PHP, request
$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
Tags: escape, PHP, sequence, \n, \r, \t
\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