Archive for the Category » Useful PHP Functions «

Validate email address in PHP

Validate your email address by regular expression in PHP. Tiny but useful function and easy to extend.

function EmailAddressValidator($Email)
{
if(eregi(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $Email))
return true;
else
return false;
}

Return full string (without broken last word)

This function will return you substring of any number of characters without broken last word. Very useful function and easy to extend as you need.

function StringWithFullWords($MainString,$NoOrCharecter=10)
{
$Tmp=str_split($MainString,1);
for($I=0;$I$NoOrCharecter)
break;
if($Tmp[$I]==” “)
$LastSpacePos=$I;
}
return substr($MainString,0,$LastSpacePos);
}

Display image or flash

The following function is able to display a file whether it’s flash or image. Short but useful function and easy to customize.
function ShowPictureOrFlash($ImageName,$ImagePath,$ImageSize,$ImageBorder=0,$ImgBorderColor=”")
{
$ImageNameWithPath=$ImagePath.$ImageName;
if (file_exists($ImageNameWithPath))
{
$ImageHeightWidth = GetImageSize($ImageNameWithPath);
$Height = $ImageHeightWidth[1];
$Width = $ImageHeightWidth[0];
if($Width>$ImageSize)
{
$multiplier=$ImageSize/$Width;
$Width = $Width*$multiplier;
$Height = $Height*$multiplier;
}
$IsItFlash=substr($ImageNameWithPath,-3,3);
if($IsItFlash==”swf”)
{
echo “”
.”"
.”"
.”"
.”"
.”";
}
else
echo ““;
}
}