Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

z re: How to protect against long urls and keep them from disrupting a page

9 views
Skip to first unread message

lawrence

unread,
Feb 17, 2004, 3:45:13 PM2/17/04
to
This is a follow-up to the thread that started here:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=abc4d8b8.0312162156.69d42b3c%40posting.google.com&rnum=2&prev=/groups%3Fq%3Dlkrubner%2540geocities.com%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DG%26scoring%3Dd

Date: 2003-12-14 21:40:23 PST
lkru...@geocities.com (lawrence) wrote in message news:<da7e68e8.03121...@posting.google.com>...
> Everyone please go look at this page, please:
>
> http://www.crookedtimber.org/archives/000977.html
>
>
> In Netscape, there is a long url that runs right off the page,
> rightward. In my own comment system, I've run into this trouble
> before. In IE, it is worse, as IE expands the box the url is in, so
> that the box continue to contain the url. On my own site, this once
> caused my main content to get covered by the box holding the comments.
>
> What are some strategies that smart programmers use to combat this
> problem? I've thought about possible solutions, like using regular
> expressions somehow, but I can't think of a strategy that will
> reliably capture all possibilities.
>
> Any ideas?

> 1. http://in.php.net/wordwrap
> 2. Use something like
><a >href="http://www.very-very-very-long-url.com/longestestestestestestestestestestestestestest.html">http://www.very-very...</a>
> or
><a >href="http://www.very-very-very-long-url.com/longestestestestestestestestestestestestestest.html">Short
>description</a>


Okay, I did try using wordwarp() for several months last year. I don't
regard it as an ideal solution because it breaks lines unevenly.

Someone suggested using tables. That is a bad idea for lots of
reasons.

I need this mostly for when users add comments to a web page, as in
the example I linked to. Sometimes they copy and paste a long url and
it ruins the page. I assume a lot of PHP programmers have had to
wrestle with this problem.

It seems to be the ideal solution is a regular expression that looks
for any string of characters that is more than, say, 80 characters
without a white space? Feedback on this idea?

Pedro Graca

unread,
Feb 17, 2004, 4:27:20 PM2/17/04
to
lawrence wrote:
> I need this mostly for when users add comments to a web page, as in
> the example I linked to. Sometimes they copy and paste a long url and
> it ruins the page. I assume a lot of PHP programmers have had to
> wrestle with this problem.
>
> It seems to be the ideal solution is a regular expression that looks
> for any string of characters that is more than, say, 80 characters
> without a white space? Feedback on this idea?

You might try to preg_replace() a long URL to a link and a shorter URL.
Something like transforming

[ I'm making this *not* wrap ]
http://www.example.com/very/long/url/here/that/leads/nowhere/but/might/in/a/real/situation.html

to

<a href="same thing as above">www.example.com/very/l ...</a>

Replacing part of a URL by "..." is OK. The full URL is still available
in the href.

I tried to do just that and came up with this code:

<?php
function reduce($url, $lenght=36) {
$x = substr($url, 7, $length);
if (strlen($url) > $length+7) $x .= ' ...';
return $x;
}

$text = 'I just found a very good source of info. Check' .
' out http://example.com/very/long/url/here/that/' .
'leads/nowhere/but/might/in/a/real/situation.html' .
' for the info.';

$text2 = preg_replace('@(http://[^ ]+)@ei',
'\'<a href="$1">\' . reduce(\'$1\', 22) . \'</a>\'',
$text);

echo $text, "<br/>\n<br/>\n";
echo $text2, "<br/>\n";
?>

--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--

lawrence

unread,
Feb 27, 2004, 3:55:50 PM2/27/04
to
Pedro Graca <hex...@hotpop.com> wrote in message news:<c0u0vo$1bbttn$1...@ID-203069.news.uni-berlin.de>...

> You might try to preg_replace() a long URL to a link and a shorter URL.
> Something like transforming
>
> [ I'm making this *not* wrap ]
> http://www.example.com/very/long/url/here/that/leads/nowhere/but/might/in/a/real/situation.html
>
> to
>
> <a href="same thing as above">www.example.com/very/l ...</a>
>
> Replacing part of a URL by "..." is OK. The full URL is still available
> in the href.
>

I like this idea very much. Thank you for the code.

0 new messages