note 82020 added to function.preg-replace
| From: | Misha at osu1 dot php dot net | Date: | Tue, 25 Mar 2008 02:45:29 +0000 |
| Subject: | note 82020 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137538@lists.php.net to get a copy of this message | ||
This is in response to iasmin at amazingdiscoveries dot org's URL text to link function. Hope
this is helpful to someone.
I played with it a bit and came up with this version (there were one or two little errors in the
regex I think, also -- it didn't allow various necessary characters).
I start with a URL in brackets (this works for my case):
[http://www.site.com/path/that/may/be_long.php?fun=1]
It returns a link of the URL after the "http://":
www.site.com/path/that/may/b...
-----------
// Cuts off long URLs at $url_length, and appends "..."
function reduceurl($url, $url_length) {
$reduced_url = substr($url, 0, $url_length);
if (strlen($url) > $url_length) $reduced_url .= '...';
return $reduced_url;
}
// Makes URLs with brackets into links
// The regex searches for "http://" or equivalent, then
various character possibilities (I don't know if it might be possible to exploit this if more
characters were allowed). The "e" after the regex allows the reduceurl() to be evaluated.
function url2link($linktext) {
$linktext = preg_replace("#\[(([a-zA-Z]+://)([a-zA-Z0-9?&%.;:/=+_-]*))\]#e",
"'<a href=\"$1\" target=\"_blank\">' .
reduceurl(\"$3\", 30) . '</a>'", $linktext);
return $linktext;
}
----
Server IP: 208.69.120.35
Probable Submitter: 70.101.113.53
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/82020
Del: integrated -- https://master.php.net/note/delete/82020/integrated
Del: useless -- https://master.php.net/note/delete/82020/useless
Del: bad code -- https://master.php.net/note/delete/82020/bad+code
Del: spam -- https://master.php.net/note/delete/82020/spam
Del: non-english -- https://master.php.net/note/delete/82020/non-english
Del: in docs -- https://master.php.net/note/delete/82020/in+docs
Del: other reasons-- https://master.php.net/note/delete/82020
Reject -- https://master.php.net/note/reject/82020
Search -- https://master.php.net/manage/user-notes.php