Connecting Tech Pros Worldwide Help | Site Map

Truncate & tag long URLs?

Philipp Lenssen
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm looking for a way to truncate/ add-spaces & tag long URLs, so that

"Check this out http://www.example.com/hello-world/foo-bar.html it's
great"

would turn into

"Check this out <a
href="http://www.example.com/hello-world/foo-bar.html">http://www.exampl
e.com/hello-world/fo...</a> it's great"

or

"Check this out <a
href="http://www.example.com/hello-world/foo-bar.html">http://
www.example.com/ hello-world/ foo-bar.html</a> it's great"

This is because my discussion forums width is restricted and long URLs
break this.

Thanks!


-------------- Current attempts...

Adds a class which I then set to display blog, fixed width, and
overflow auto (creates newline problem though):

function tagLinks($str)
{
$pattern =
'#(^|[^\"=]{1})(http://|https://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>
]|$)#sm';
$str = preg_replace($pattern, "\\1<a class=\"forumlink\"
href=\"\\2\\3\">\\2\\3</a>\\4", $str);
$str = str_replace("\">http://", "\">", $str);
return $str;
}

Cuts off after domain (which is really bad at times):

function tagLinks($str)
{
$str = str_replace("-http:", "-temp:", $str);
$str = str_replace("<br />", " <br />", $str);
$str = preg_replace(
'/(http|https|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<em><a
href="\\0">\\4</a></em>', $str );
$str = str_replace(" <br />", "<br />", $str);
$str = str_replace("-temp:", "-http:", $str);
return $str;
}


You can see the forum at <http://blog.outer-court.com/forum>.
Pedro Graca
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Truncate & tag long URLs?


Philipp Lenssen wrote:[color=blue]
> I'm looking for a way to truncate/ add-spaces & tag long URLs, so that
>
> "Check this out http://www.example.com/hello-world/foo-bar.html it's
> great"
>
> would turn into
>
> "Check this out <a
> href="http://www.example.com/hello-world/foo-bar.html">http://www.exampl
> e.com/hello-world/fo...</a> it's great"[/color]

Check out this thread:

http://groups.google.com/groups?thre...ing.google.com

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Philipp Lenssen
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Truncate & tag long URLs?


Pedro Graca wrote:
[color=blue]
> Philipp Lenssen wrote:[color=green]
> > I'm looking for a way to truncate/ add-spaces & tag long URLs[/color][/color]
[color=blue]
>
> http://groups.google.com/groups?thre...245.39877e38%4
> 0posting.google.com[/color]

Thanks, that works great!

--
Google Blogoscoped
http://blog.outer-court.com
Closed Thread