787 snippets from 1631 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(143)
JavaScript
(125)
Java
(66)
VBSCRIPT
(58)
String
(44)
CSS
(31)
File
(29)
CSharp
(28)
HTML
(27)
mysql
(27)
C
(24)
VB.NET
(24)
python
(24)
CPlusPlus
(23)
groovy
(23)
New Snippets
xoops token auto ...
Detect Adblock
Concatenar campos...
fopen
Unique random key
get number of cha...
Find a File
Find a Directory
List Directory
File uploading in...
Venture Capital Jobs
New Members
mcheung63
cicero
mycodeofshailendra
nostromo
KennethCC
me
jamesmcm
Can
Kelmi
ysg
Top Members
dannyboy
sundaramkumar
mattrmiller
Pio
i_kenneth
ASmith
ctiggerf
sehrgut
bertheymans
SCoon
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'