1934 snippets from 2406 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(160)
stablo
(139)
JavaScript
(138)
Battery
(96)
C
(74)
binarno
(72)
Dell
(68)
Java
(68)
adapter
(65)
polje
(62)
strukture
(60)
VBSCRIPT
(60)
HP
(50)
String
(45)
Batterie
(39)
New Snippets
CompCalc
Nettoyage de tapi...
Nettoyage de mate...
Zaglavlje za stab...
Jewellery trade f...
Industrial trade ...
Batterie asus a42...
Batera Lenovo T...
Batera Lenovo T...
Pokazivac zadatak...
Venture Capital Jobs
New Members
pimteam
zjakupec
Pushkartyagi
niprasnja
lzodan
mlcorak
bonikolic
mezestic1
rekapec
Kasimu
Top Members
dannyboy
sundaramkumar
mattrmiller
all-battery
Pio
Cloudgen
i_kenneth
ASmith
mycodeofshailendra
ctiggerf
Home
/
Snippets
/
PHP - Make URLs clickable (And short down)
/
Comments
PHP - Make URLs clickable (And short down)
Snippet Menu
Revisions
Comments
Related Snippets
Add to Favorites
Email Snippet
Download Snippet
Print Snippet
Blog Snippet
snippet
|
revisions
|
comments
|
related
|
Add to Favorites
|
email
download
|
print
|
blog it
New Comment
Revision
Fri. Mar. 9th, 2007 4:33 AM
Nico
Here's a little revision.
function
parse_urls
(
$text
,
$maxurl_len
=
35
,
$target
=
'_self'
,
$only_short_down
=
false
)
{
if
(
!
$only_short_down
)
{
return
preg_replace
(
'/((ht|f)tps?:
\/
\/
[^
\s
\r
\n
\t
<>"
\'
\!
\(
\)
]+)/ie'
,
"'<a href=
\"
$1
\"
title=
\"
$1
\"
target=
\"
$target
\"
>'. parse_urls(
\"
$1
\"
, $maxurl_len, $target, true) .'</a>'"
,
$text
)
;
}
if
(
$maxurl_len
AND
strlen
(
$text
)
>
$maxurl_len
)
{
$offset1
=
ceil
(
0.65
*
$maxurl_len
)
-
2
;
$offset2
=
ceil
(
0.30
*
$maxurl_len
)
-
1
;
$text
=
substr
(
$text
,
0
,
$offset1
)
.
'...'
.
substr
(
$text
, -
$offset2
)
;
}
return
$text
;
}
Reply
2nd Revision
Wed. Apr. 4th, 2007 7:58 AM
Nico
Just a little modification for the pattern, so that it ONLY makes URLs clickable that aren't clickable already.
Just replace the old pattern with this one:
'/(?<!href=["
\'
])((ht|f)tps?:
\/
\/
[^
\s
\r
\n
\t
<>"
\'
\!
\(
\)
]+)/ie'
Reply
New Comment
function parse_urls($text, $maxurl_len = 35, $target = '_self', $only_short_down = false)
{
if (!$only_short_down)
{
return preg_replace('/((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\!\(\)]+)/ie', "'<a href=\"$1\" title=\"$1\" target=\"$target\">'. parse_urls(\"$1\", $maxurl_len, $target, true) .'</a>'", $text);
}
if ($maxurl_len AND strlen($text) > $maxurl_len)
{
$offset1 = ceil(0.65 * $maxurl_len) - 2;
$offset2 = ceil(0.30 * $maxurl_len) - 1;
$text = substr($text, 0, $offset1) . '...' . substr($text, -$offset2);
}
return $text;
}
Just replace the old pattern with this one:
'/(?<!href=["\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\!\(\)]+)/ie'