Custom PHP common functions
Useful standalone php functions
I am listing a collection of PHP functions I have used over the years to organise and speed up my coding. I am now looking at re-writting these using PHP 5 and using PEAR for some standard functions. So thought this would be a good time to document them and hopefully they may help somebody with there work in the future.
Some people would argue that small standalone functions like these are no longer needed due to function libraries (like PEAR) But if you are just building a small site with limited functions I don't feel a large library is always needed.
Truncation Function
Requires two parameters the phrase as a string and a number as an integer.
// For truncating article descriptions
function trunc($phrase, $max_words) {
$phrase_array = explode(' ',$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase =
implode(' ',array_slice($phrase_array, 0, $max_words)) . '...';
return $phrase;
}
?>
Form email input checking function
I use these two functions on all forms submitted that require email addresses.
function emailcheck($emailcheck) {
if (isset ($emailcheck) &&!empty ($emailcheck) ) {
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)
(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
if (!eregi($regexp, $emailcheck)){
error("The email should only contain Alphabetical
\\nletters, Numeric Characters, @, - and _");
}
}
}
//Check for Spam injections
function spamcheck($email) {
$header_injection_attempts = array(
"bcc:",
"cc:",
"to:",
".exe",
"content-type:",
"mime-version:",
"multipart/mixed",
"content-transfer-encoding:"
);
// lowercase the email
$email_lower = strtolower($email);
// innocent until proven guilty
$injection_attempted = false;
foreach($header_injection_attempts as $attempt){
// check the email for each possible attempt
if(strpos($email_lower, $attempt)!==false){
// we found something bad being attempted
$injection_attempted = true;
// get out of the loop
break;
}
}
if($injection_attempted){
// don't send the email
error("A spam injection attempt has been discovered.");
}
}
Mailto Function
I have this setup to send HTML emails but you can change the settings.
// email user a message
function emailuser($email, $subject, $message) {
$to = $email;
$header = "From: contact@paulwest.com" . "\r\n";
// To send HTML mail, the Content-type header must be set
$header .= 'MIME-Version: 1.0'
. "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1'
. "\r\n";
mail($to, $subject, $message, $header);
}
Convert Time and date to mktime functions
Quite often you may have a date or time (or both) from a MySQL database that you need in mktime format for using with the date() function. The functions below do this for you.
function timestampdate($date) {
$aDate = explode("-",$date);
$datetime = mktime(0, 0, 0, $aDate[1], $aDate[2], $aDate[0]);
return $datetime;
}
function timestamptime($time) {
$aTime = explode(":",$time);
$datetime = mktime($aTime[0], $aTime[1], $aTime[2], 1, 1, 2007);
return $datetime;
}
function timestampboth($date, $time) {
$aDate = explode("-",$date);
$aTime = explode(":",$time);
$datetime = mktime($aTime[0], $aTime[1], $aTime[2],
$aDate[1], $aDate[2], $aDate[0]);
return $datetime;
}
Published 09.10.08

News Feed