| Jörn 'TheSpook' Berkefeld 2006-12-15, 7:00 pm |
| Hello,
I tried posting the below msg but unfortunately I always received the notice
“Your note contains a bit of text that will result in a line that is too
long, even after using wordwrap().”
I tried to shorten it but as you probably know, php can’t be shorten all the
time.
That’s where I wanted to leave the msg: http://de.php.net/pcre
Regards
Jörn 'TheSpook' Berkefeld
Managing Director
Webmaster
________________________________________
________
European Cyber League
www.ec-league.org
IRC: #ec-league @ QuakeNet
________________________________________
________
THE MESSAGE I TRIED POSTING:
========================================
=====
i know there's been quite a lot of submissions on retrieving emails and urls
from text but
i couldnt but notice that they all had errors that prevented them from
working correctly.
since im using an javascript to keep emails on my website safe from spider
bots i wanted
mails in comments to be save as well...
you might notice theres also a function included that shortens long url
descriptions.
i thought some of you might like that as well.
unfortunately i had to use a second check function for the email catcher...
it simply wouldnt
work any other way for me. feel free to put both checks, the preg_replace
and the ereg function in one test...
<?php
function formaturl($url, $title='',$leading_space='', $maxwidth=60,
$width1=40, $width2=-15) {
$url =
str_replace("\r",'',str_replace("\rn",'',str_replace("\n",'',$url)));
if(!trim($title)) $title=$url;
if(!preg_match("/[a-z]:\/\//si", $url)) $url = "http://$url";
if(strlen($title)>$maxwidth) $title =
substr($title,0,$width1)."...".substr($title,$width2);
return $leading_space."<a href='$url'
target='_blank'>".str_replace("\\\"", "\"", $title)."</a>";
}
function format_email($email,$description='',$lea
ding_space='') {
$email = trim($email);
if(!$description) $description = $email;
if( eregi(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$" ,
$email) )
return $leading_space."<a
href='mailto:$email'>$description</a>";
else
return $leading_space.$email;
}
function prepare_code($out) {
global $searcharray,$replacearray,$conn;
if(!isset($searcharray) && !isset($replacearray)) {
# [url]www. [url]http:// ftp:// https:// ftps://
$searcharray[]=
"/\[url=(['\"]?)([^\"']*)\\1](.*)\[\/url\]/esiU";
$replacearray[]= "formaturl('\\2','\\3')";
$searcharray[]= "/\[url]([^\"]*)\[\/url\]/esiU";
$replacearray[]= "formaturl('$1')";
$searcharray[] =
"/(^|\s)(www)\.([^\s<>\.]+)\.([^\s\n<>]+)/esmi";
$replacearray[] = "formaturl('$2.$3.$4','','$1')";
$searcharray[] =
"/(^|\s)(http|ftp)(s)?:\/\/([^\s<>\.]+)\.([^\s<>]+)/esmi";
$replacearray[] = "formaturl('$2$3://$4.$5','','$1')";
# [EMAIL=xxxx] [email] test@test.com
$searcharray[]=
"/\[email=(['\"]?)([^\"']*)\\1](.*)\[\/email\]/esiU";
$replacearray[]= "format_email('$3','$2')";
$searcharray[]= "/\[email]([^\"]*)\[\/email\]/esiU";
$replacearray[]= "format_email('\\1')";
$searcharray[]= "/(^|\s)([_a-z0-9-\.]+)@([_a-z0-9-\.]+)/esmi";
$replacearray[]= "format_email('$2@$3','','$1')";
# can't get the following lines to work - should replace the 2 lines above
plus
# the ereg check in the format_email function...
# does not work if the email looks like this: test@does.not.work.com
# for some reason it truncates to test@does.com
# i figured it has something to do with finding (\.[a-z0-9-]+) more than
once? anyone an idea?
# $searcharray[] =
"/(^|\s)([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,
4})/esi";
# $replacearray[] = "format_email('$2$3@$4$5$6','','$1')";
}
$out = preg_replace($searcharray, $replacearray, $out);
return $out;
}
?>
note that i stripped the javascript function i mentioned above cause the
script is easier to read this way.
I also sometimes used the \\1 notation for variables and sometimes the new
$1 notation in the replacestring.
As mentioned in the description of the preg function above this makes no
difference at all.
========================================
=====
|