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?
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 =--
I like this idea very much. Thank you for the code.