Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

Thursday, October 11, 2012

Protect email address in web content.

While building web content we may need to include email address. In this case spam bots are may access our web page and collect email address inside our web content. based on this email address extracting script.

The only way to protect Email address inside the content is convert all email address into image.

Here is the solution to convert email address into dynamic text images



define("F_SIZE", 10);
define("F_ANGLE", 0);
define("F_FONT", "verdana.ttf");

$text = base64_decode(base64_decode($_GET['id']));

$_bx = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$s = preg_split("/\n]+/", $text);  // Array of lines
$nL = count($s);  // Number of lines

$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+2+($nL>1?($nL*$Leading):0):$H;

$img = @imagecreate($W+8, $H) or die("Cannot Initialize new GD image stream");

$white = imagecolorallocate($img, 255,255,255);
$txtColor = imagecolorallocate($img, 255,50,0);

// Adjust padding right:
$bgwhite = imagecolorallocatealpha($img,238,238,238,0);
imagefilledrectangle($img, 0, 0,$W,$H, $bgwhite);

$alpha = "".range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);

// Use the string to determine the height of a line
$_b = imageTTFBbox(F_SIZE, F_ANGLE, F_FONT, $alpha);
$_H = abs($_b[5]-$_b[3]);
$__H = 0;

// Use the string to determine the width of a line
$_b = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$_W = abs($_b[2]-$_b[0]);

// Final width and height
$_X = abs($W/2)-abs($_W/2);
$__H += $_H;

imagettftext($img, F_SIZE, F_ANGLE, $_X, $__H, $txtColor, F_FONT, $text);

header("Content-Type: image/png");


// Keep line breaks inside textarea
$content=preg_replace("/[\n]/", '<br />', $content);
// Find All email ids inside content

// Replace as image every email address
foreach($emails[0] as $email)
$content=str_replace($email,'<img src="email-to-image.php?id='.base64_encode(base64_encode($email)).'">',$content);
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
body,td,th {
 font-family: Verdana, Geneva, sans-serif;
 font-size: 12px;
 color: #000;
body {
 margin-left: 5px;
 margin-top: 5px;
 margin-right: 5px;
 margin-bottom: 5px;
 margin:0 auto;

textarea { width:750px; font-size:15px; font-family:Verdana, Geneva, sans-serif;}

 border:1px solid #CCC; 

#content img



<h1>Protect Email Address inside web content</h1>
<form action="" method="post">
<textarea cols="70" rows="10" name="content">
content mixed with any email address. that email address text will be converted into as image. for example we have mail in this text area it will be return as image after the compilation. any no of occurrence will automatically replaced as image 

For Example:

<br /><br />

<input type="submit" value="Compile" name="submit" /><br />
<br />


<div id="content">
<?php echo @$content; ?>

Download This Script     Live Demo     Download Script

Wednesday, October 10, 2012

Extract Email ID from Content PHP

In many, CMS development, We may often to check whether any email was found inside the content in this case. Here is the best solution to extract email ids from content using the power of regular expression.

Regular Expression to extract Email IDs from Content.


PHP function to extract All Regular Expression Matches


Sample Content.txt with email id mixed html

<p>example for extracting email id <b></b> 
from entire content it mean how many <b></b> 

//Extract Email ID from content.txt

$whois = file_get_contents("content.txt");

echo "<pre>";




    [0] =>
    [1] =>
    [2] =>

Tuesday, August 16, 2011

Check Valid Domain Name and Valid TLD in PHP

Check the domain name is valid Domain Name and Domain extension is valid TLD.  When working this task I want to find the solution to check the domain name is valid TLD and the valid domain name. with the power of Regular Expression. Here is the solution to check the given domain name is valid TLD and valid name.

Following function will full fill the following needs:
  • Domain Name Must contain Alpha Numeric 
  • Only special character (-) hyphen. is allowed on domain names.
  • Check the generic domain extension (.com, .edu, .gov, .int, .mil, .net, and .org)
  • All International domain extensions (TLDs) approved by ICANN


// @ Desc: Function to check the Given Domain Name is Valid Domain Name and Valid TLD
// @ Parm: domain name : eg:
// @ Return: 0 or 1 (for Valid Domain Name)
function is_valid_domain_extension($domain)
	return preg_match('/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i',$domain);

// Example 
// .MZ the domain for Republic of Mozambique
// I aware of the Region when I am working for this function.
echo is_valid_domain_extension(""); // 


Sunday, August 14, 2011

Regular Expressions in PHP for From Validation

As you working in any project regular expressions are very usefull to Form Validation,
here is some of Regular expression which is frequently used for form validation:

  • Email Validation
  • Mobile/Phone Number validation
  • Strong Password validation
  • IP Address Validation
  • Uploaded Image File Validation


// Valid Email Address Number or Not

		echo "<br>Valid Email Address";
		echo "<br>Invalid ";
// Valid Phone Number or Not

		echo "<br>Valid Phone Number";
		echo "<br>Invalid Phone Number";

// Valid Strong Password or Not (Check password with Digits Caps Small Letters symols )

		echo "<br>Strong Password";
		echo "<br>Weak Password";

// Valid IPv4 Address or Not to

		echo "<br>Valid IP Address";
		echo "<br>Invalid IP Address";

// Valid Image or Not (jpg|gif|png)

		echo "<br>Valid Image file";
		echo "<br>Invalid Image file";