Friday, November 16, 2007

RegEx email parser

Split email formats in pieces through preg_match:

$str=' (|(([^a-zA-Z0-9_\-.]|)(.*?)(|[^a-zA-Z0-9_\-.]+?)([a-zA-Z0-9_\-.]+?)(|[^a-zA-Z0-9_\-.]+?)))(|[^a-zA-Z0-9_\-.])([a-zA-Z0-9_\-.]+?)(|[^a-zA-Z0-9_\-.])@(.+?)\.([^>]+)';

Version #2:

if (preg_match('/^[^\W][a-zA-Z0-9_\.\-]+(\.[a-zA-Z0-9_\-]+)*\@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}$/',$item['email'])) {
...
}

Tuesday, July 31, 2007

Format long texts


/**
* Cut a text so it won't have more than $max_length chars and any word should not have more than $max_word chars
*
* @param unknown_type $length_limit
* @param unknown_type $length_word
*/
function format_long_text($str, $max_length=99999, $max_word = 60){
//limit string length
$has_tail = false;
if(strlen($str) > $max_length){
$new_str = substr($str, 0, $max_length);
//search for non-alpha char
for($i = $max_length; $i < strlen($str) && ctype_alpha($str[$i]); $i++) $new_str .= $str[$i];
$str = $new_str;
$has_tail = true;
}

if(3 < $max_word && $max_word < $max_length){
do{
$words = explode(' ', $str);
$do_loop = false;
foreach ($words as $word){

if(strlen($word) > $max_word){
//bug in IE: point followed by char does not split the text
$fixed_word = str_replace('.', '. ', $word);

$new_word = '';
$j = 0;
for($i = 0; $i < strlen($fixed_word); $i++){
$j = (' ' == $fixed_word[$i]?0:$j+1);

if(($j+1)%$max_word==0){
$new_word .= ' ' . $fixed_word[$i];
}else{
$new_word .= $fixed_word[$i];
}
}
$str = str_replace($word, $new_word, $str);
$do_loop = true;
break;
}
}
}while($do_loop);
}

return $str . ($has_tail?'..':'');
}


Wednesday, April 25, 2007

META tag library

This library if for obtaining content for the META TAGS like keywords, description and also for computing tags to be used in searches based on the given text.

http://www.worldsview.com/metataggen_library.zip

Friday, March 16, 2007

Monday, March 12, 2007

php cache


<?php

$url = 'http://www.site.com/file.php';
$dest_file = 'footercache.txt'; //be sure it is chmod-ed to 0666 !!
//Note: for requests by keyword use a cache folder whose rights are 0777 and define the destination file as
//$dest_file = CACHE_FOLDER . '/cache-' . preg_replace('/[^a-z0-9]+/','_', $keyword) . '.txt';
//this may need changes on the line checking the length of the dest_file


$pagesource = request_cache($url, $dest_file, 3600*24*7);
echo $pagesource;

function request_cache($url, $dest_file, $timeout=7200) {
if(strlen($dest_file) > 100) $dest_file = substr($dest_file, 20, 60) . substr($dest_file, strlen($dest_file) - 4);//keep some chars and the probable extension

if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
$data = @file_get_contents($url);
if ($data !== false) {
$tmpf = tempnam('/tmp','YWS');
$fp = @fopen($tmpf,"w");
@fwrite($fp, $data);
@fclose($fp);
if(file_exists($dest_file)) @unlink($dest_file);
rename($tmpf, $dest_file);
}else{
touch($dest_file);//update its date only
}
} else {
$data = file_get_contents($dest_file);
}
return($data);
}

?>