Saturday, July 9, 2016

php function to sanitize or clean your url

Function to remove unwanted characters from your dynamically generated URLs


function sanitizeURL ($URL, $tolower = true){
$find = array("/\s+/", "/[-]+/", "/\\\/", "/'/");
$replace_with = array("-", "-", "", "");
$URL = preg_replace($find, $replace_with, $URL);
if($tolower){
$URL = strtolower($URL);
}
return strtolower($URL);
}

Demo

if i send

$url = 'http://www.abc.com/'.NormalizeURL('names in Advertising /Entertainment/ Media/ Publishing at abc.com');


echo $url;

OUTPUT

http://www.abc.com/names-in-advertising-/entertainment/-media/-publishing-at-abc.com

Friday, July 8, 2016

Dynamic Parse Mail Template

Most of us want a mailing system with template ...Simple Mailing does not always deliver best result


While we use Library like PHPMailer to send mail we Need to send the data  as text/HTML and that also dynamic Data to be sent in proper format .

Here is Example of How you can dynamically parse a mail template.


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ABS_PATH = ABSOLUE PATH
// DS = SAPERATOR
// MAIL_TEMPLATE_PATH = TEMPLATE FILE PATH

argument 1 is name of the html or htm file to be parsed
argument 2 is the array which is to be parsed.

function ParseMailTemplate($Template, $Custom=""){
$GeneralKywords = array();
$GeneralKywords["SERVER_ROOT"]=SERVER_ROOT;

$f = fopen(ABS_PATH.DS.MAIL_TEMPLATE_PATH."/".$Template,"r");
if(!$f){
return NULL;
}
$TemplateBody = fread($f,filesize(ABS_PATH.DS.MAIL_TEMPLATE_PATH."/".$Template));
fclose($f);

$HTMLBody=$TemplateBody;

if(is_array($Custom)){
foreach($Custom as $Find=>$ReplaceWith){
$TemplateBody = str_replace("{".$Find."}",$ReplaceWith,$TemplateBody);
}
}

foreach($GeneralKywords as $Find=>$ReplaceWith){
$TemplateBody = str_replace("{".$Find."}",$ReplaceWith,$TemplateBody);
}

return $TemplateBody;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Demo template file : contact.html

Here variable in curly bracket are sent via array in function

Example

$mail_body = $this->ParseMailTemplate("contact.html",
array("name"=>$_POST['name'],
                                                                                "city"=>$_POST['city'],
                                                                                "phone"=>$_POST['phone'],
         "email"=>$_POST['email'],
 "msgbox"=>$_POST['msgbox'],
 )
 );


Then send the $mail_body as message in your mail .

All Done
Thanks