2007/06/01

regular expresions / eval / php

revisar, tomado de:

http://www.marotori.com/blog/?cat=10



Archive for the ‘PHP’ Category
Using PHP to highlight links in html
Sunday, March 4th, 2007 by Rob Thomson

Seems to me that there are loads of code samples that show you how to ‘auto highlight’ email addresses and web addresses in a string. Useful, but what happens when the link is already surrounded by an A tag?

I put together this little function (a little dirty but effective) to ‘ignore’ links that are already surrounded by an A tag. This prevents any unexpected double linking of your code.




function findlinks($string){

$string = $string ;

# regular expresion to find matches
$href_regex =”<”; // 1 start of the tag
$href_regex .=”\s*”; // 2 zero or more whitespace
$href_regex .=”a”; // 3 the a of the tag itself
$href_regex .=”\s+”; // 4 one or more whitespace
$href_regex .=”[^>]*”; // 5 zero or more of any character that is _not_ the end of the tag
$href_regex .=”href”; // 6 the href bit of the tag
$href_regex .=”\s*”; // 7 zero or more whitespace
$href_regex .=”=”; // 8 the = of the tag
$href_regex .=”\s*”; // 9 zero or more whitespace
$href_regex .=”[\”‘]?”; // 10 none or one of ” or ‘
$href_regex .=”(”; // 11 opening parenthesis, start of the bit we want to capture
$href_regex .=”[^\”‘ >]+”; // 12 one or more of any character _except_ our closing characters
$href_regex .=”)”; // 13 closing parenthesis, end of the bit we want to capture
$href_regex .=”[\”‘ >]”; // 14 closing chartacters of the bit we want to capture

$regex = “/”; // regex start delimiter
$regex .= $href_regex; //
$regex .= “/”; // regex end delimiter
$regex .= “i”; // Pattern Modifier - makes regex case insensative
$regex .= “s”; // Pattern Modifier - makes a dot metacharater in the pattern
// match all characters, including newlines
$regex .= “U”; // Pattern Modifier - makes the regex ungready

# first find and subst out all text area content
preg_match_all($regex, $string, $regs);
# replace all textarea content with a custom tag
$count = “1″;
if(is_array($regs[”0″])){
foreach($regs[”0″] as $tag){
$string = str_replace($tag,”{link-” . $count . “}”,$string);
$textarea[] = array(”field” => “{link-” . $count . “}”,”data” => $tag);
$count++;
}
}

$string = preg_replace(”/([\w\.]+)(@)([\S\.]+)\b/i”,”$0“,$string);
$string = preg_replace(”/((http(s?):\/\/)|(www\.))([\S\.]+)\b/i”,”$2$4$5“, $string);

# reassemble to handle php code input into text areas
if(is_array($textarea)){
foreach($textarea as $x){
$string = str_replace($x[field],$x[data],$string);
}
}

return trim($string);
}
?>



Anyone got anything better than this?

Posted in PHP | No Comments »
Using EVAL to parse an html file
Sunday, March 4th, 2007 by Rob Thomson

Every now and again I stumble upon a little bit of code that just ‘works’. Anyone who as ever coded in PHP and tried to ‘eval’ and html file is almost certain to have ended up a bit frustrated. It would seem that eval can only process pure PHP, and not a file that has any html tags etc in it.

Never mind! After trawling the net for a while I found this great code snippet that does the trick.



function eval_buffer($string) {
ob_start();
eval("$string[2];");
$return = ob_get_contents();
ob_end_clean();
return $return;
}

function eval_print_buffer($string) {
ob_start();
eval(”print $string[2];”);
$return = ob_get_contents();
ob_end_clean();
return $return;
}

function eval_html($string) {
$string = preg_replace_callback(”/(<\?=)(.*?)\?>/si”,
“eval_print_buffer”,$string);
return preg_replace_callback(”/(<\?php|<\?)(.*?)\?>/si”,
“eval_buffer”,$string);
}

?>


Works perfectly!

No comments: