Tuesday, March 23, 2010

function that removes the HTML tags along with their contents in php

function strip_tags_content($text, $tags = '', $invert = FALSE) {

preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);

if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?@si', '', $text);
}
return $text;
}
Sample text:
$text = 'sample text with
tags
';

Result for strip_tags($text):
sample text with tags

generating Unique random key in php

function get_cripta()
{
$year = date('Y');
$month = date('m');
$day = date('d');
$hour = date('H');
$min = date('i');
$sec = date('s');
$coef = rand(1,99999);
$quo1 = ($year*$month*$day);
$quo2 = ($hour+$min+$sec);
$result = ($quo1*$quo2)*$coef;
$final = md5($result);
return $final;
}

Date and Time Zone in php

Function to get Date format by time zone:
function get_date(DateTime $time)
{

$t = clone $time;
// set time zone here
$t->setTimezone(new DateTimeZone("America/Denver"));
return $t->format("Y-m-d H:i:s");
}
?>
// usages
$date ="2008-08-03 14:52:10";
$datetime = date_create($date);
echo get_ebay_UTC_8601($datetime);
?>