here is a nice simple function that uses preg_replace to clean a string, and make sure it returns only normal characters.
It is another way of cleaning a string, similar to the method i used in Generate Safe File Name in PHP using ereg_replace function.
Not sure really which is better i think one of them may be faster than the other. Please feel free to comment and let me know.
return only normal characters (a-z 0-9) php function using preg_replace
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Wednesday, June 22, 2011
Friday, December 11, 2009
PHP – Automated Creation of Thumbnails
[PHP CODE]
// define the base image dir
$base_img_dir = "./";
// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png) type of output
// q(100) quality of jpeg
// find tags
preg_match_all("/\+*(([a-z])\(([^\)]+)\))\+*/", $QUERY_STRING,
$matches, PREG_SET_ORDER);
// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "[0-9a-zA-Z]{13}",
"w" => "[0-9]+%?",
"h" => "[0-9]+%?",
"x" => "[0-9]+",
"y" => "[0-9]+",
"t" => "jpg|png",
"q" => "1?[0-9]{1,2}" );
// check tags and save correct values in array
for ($i=0; $i if (isset($check[$matches[$i][2]])) {
if (preg_match('/^('.$check[$matches[$i][2]].')$/',
$matches[$i][3])) {
$tags[$matches[$i][2]] = $matches[$i][3];
}
}
}
function notfound() {
header("HTTP/1.0 404 Not Found");
exit;
}
// check that filename is given
if (!isset($tags["f"])) {
notfound();
}
// check if file exists
if (!file_exists($base_img_dir.$tags["f"])) {
notfound();
}
// retrieve file info
$imginfo = getimagesize($base_img_dir.$tags["f"]);
// load image
switch ($imginfo[2]) {
case 2: // jpg
$img_in = imagecreatefromjpeg($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "jpg";
}
break;
case 3: // png
$img_in = imagecreatefrompng($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "png";
}
break;
default:
notfound();
}
// check for maximum width and height
if (isset($tags["x"])) {
if ($tags["x"] < imagesx($img_in)) {
$tags["w"] = $tags["x"];
}
}
if (isset($tags["y"])) {
if ($tags["y"] < imagesy($img_in)) {
$tags["h"] = $tags["y"];
}
}
// check for need to resize
if (isset($tags["h"]) or isset($tags["w"])) {
// convert relative to absolute
if (isset($tags["w"])) {
if (strstr($tags["w"], "%")) {
$tags["w"] = (intval(substr($tags["w"], 0, -1)) / 100) *
$imginfo[0];
}
}
if (isset($tags["h"])) {
if (strstr($tags["h"], "%")) {
$tags["h"] = (intval(substr($tags["h"], 0, -1)) / 100) *
$imginfo[1];
}
}
// resize
if (isset($tags["w"]) and isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $tags["h"];
} elseif (isset($tags["w"]) and !isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $imginfo[1] * ($tags["w"] / $imginfo[0]);
} elseif (!isset($tags["w"]) and isset($tags["h"])) {
$out_w = $imginfo[0] * ($tags["h"] / $imginfo[1]);
$out_h = $tags["h"];
} else {
$out_w = $tags["w"];
$out_h = $tags["h"];
}
// new image in $img_out
$img_out = imagecreate($out_w, $out_h);
imagecopyresized($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out),
imagesy($img_out), imagesx($img_in), imagesy($img_in));
} else {
// no resize needed
$img_out = $img_in;
}
// check for a given jpeg-quality, otherwise set to default
if (!isset($tags["q"])) {
$tags["q"] = 75;
}
// returning the image
switch ($tags["t"]) {
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, "", $tags["q"]);
exit;
case "png":
header("Content-type: image/png");
imagepng($img_out);
exit;
default:
notfound();
}
[PHP CODE]
cant get the dam thing to show properly, grab the source here: Elgene
// define the base image dir
$base_img_dir = "./";
// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png) type of output
// q(100) quality of jpeg
// find tags
preg_match_all("/\+*(([a-z])\(([^\)]+)\))\+*/", $QUERY_STRING,
$matches, PREG_SET_ORDER);
// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "[0-9a-zA-Z]{13}",
"w" => "[0-9]+%?",
"h" => "[0-9]+%?",
"x" => "[0-9]+",
"y" => "[0-9]+",
"t" => "jpg|png",
"q" => "1?[0-9]{1,2}" );
// check tags and save correct values in array
for ($i=0; $i
if (preg_match('/^('.$check[$matches[$i][2]].')$/',
$matches[$i][3])) {
$tags[$matches[$i][2]] = $matches[$i][3];
}
}
}
function notfound() {
header("HTTP/1.0 404 Not Found");
exit;
}
// check that filename is given
if (!isset($tags["f"])) {
notfound();
}
// check if file exists
if (!file_exists($base_img_dir.$tags["f"])) {
notfound();
}
// retrieve file info
$imginfo = getimagesize($base_img_dir.$tags["f"]);
// load image
switch ($imginfo[2]) {
case 2: // jpg
$img_in = imagecreatefromjpeg($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "jpg";
}
break;
case 3: // png
$img_in = imagecreatefrompng($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "png";
}
break;
default:
notfound();
}
// check for maximum width and height
if (isset($tags["x"])) {
if ($tags["x"] < imagesx($img_in)) {
$tags["w"] = $tags["x"];
}
}
if (isset($tags["y"])) {
if ($tags["y"] < imagesy($img_in)) {
$tags["h"] = $tags["y"];
}
}
// check for need to resize
if (isset($tags["h"]) or isset($tags["w"])) {
// convert relative to absolute
if (isset($tags["w"])) {
if (strstr($tags["w"], "%")) {
$tags["w"] = (intval(substr($tags["w"], 0, -1)) / 100) *
$imginfo[0];
}
}
if (isset($tags["h"])) {
if (strstr($tags["h"], "%")) {
$tags["h"] = (intval(substr($tags["h"], 0, -1)) / 100) *
$imginfo[1];
}
}
// resize
if (isset($tags["w"]) and isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $tags["h"];
} elseif (isset($tags["w"]) and !isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $imginfo[1] * ($tags["w"] / $imginfo[0]);
} elseif (!isset($tags["w"]) and isset($tags["h"])) {
$out_w = $imginfo[0] * ($tags["h"] / $imginfo[1]);
$out_h = $tags["h"];
} else {
$out_w = $tags["w"];
$out_h = $tags["h"];
}
// new image in $img_out
$img_out = imagecreate($out_w, $out_h);
imagecopyresized($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out),
imagesy($img_out), imagesx($img_in), imagesy($img_in));
} else {
// no resize needed
$img_out = $img_in;
}
// check for a given jpeg-quality, otherwise set to default
if (!isset($tags["q"])) {
$tags["q"] = 75;
}
// returning the image
switch ($tags["t"]) {
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, "", $tags["q"]);
exit;
case "png":
header("Content-type: image/png");
imagepng($img_out);
exit;
default:
notfound();
}
[PHP CODE]
cant get the dam thing to show properly, grab the source here: Elgene
Tuesday, September 15, 2009
PHP counting the # of word occurance in a string
$string = 'hi there [hello]this is my hello message[/hello][hello]this is another hello message[/hello]';
// Count the number of occurences:
$count = preg_match_all( '/\\[hello\\]/', $string, $dummy );
// Delete [hello] if not closed
// First replace closed matches, then delete remaining, then replace back
$string = preg_replace( '/\\[hello\\](.*)\\[\\/hello\\]/U', '[qwerty]$1[/qwerty]', $string );
$string = str_replace( '[hello]', '', $string );
$string = str_replace( 'qwerty', 'hello', $string );
echo "Count is: $count$string";
?>
Wednesday, September 2, 2009
parsing the google trends atom feed rss
$feed = simplexml_load_file('http://www.google.com/trends/hottrends/atom/hourly');
$children = $feed->children('http://www.w3.org/2005/Atom');
$parts = $children->entry;
foreach ($parts as $entry) {
$details = $entry->children('http://www.w3.org/2005/Atom');
$dom = new domDocument();
@$dom->loadHTML($details->content);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$url = $anchor->getAttribute('href');
$urltext = $anchor->nodeValue;
echo 'Link: ' . $urltext . ' ';
}
}
Subscribe to:
Posts (Atom)
