On Jul 1, 2008, at 4:27 PM, Nate Tallman wrote:

If you want to do it on the php side, I would do something like this:

<a href="$fullURL">substr($fullURL, 0, 9)."..."</a>

It would provide a valid link using the full url, but chop off everything
after the 10th character and replace with a "...".

Nate

I've seen some sites(/browsers?) do something similar to this - they show the first of the URL and some of the last. For example...

<?php
$url = "http://www.letshaveareallylongurl.com/somedirectory/";.
       "somelocation/someplace/32j1580/ksaladfji/".
       "dji23adf/adfjadf/dja9Jkda.html";
$len = strlen ($url);
$shortLen = 40;
$longLen = 100;

if ($len > ($shortLen + 10)) {
    if ($len > $longLen) {
        // Show first and last of the url
$newUrl = substr ($url, 0, $shortLen) . '...' . substr ($url, -10);
    } else {
        // Only show first
        $newUrl = substr ($url, 0, $shortLen+7) . '...';
    }
} else { $newUrl = $url; }

echo "<a href=\"$url\">$newUrl</a>";
?>

This output would be:
http://www.letshaveareallylongurl.com/so...9Jkda.html

Of course, you can change the lengths around to suit your needs. Just another way to skin the cat.

~Philip


On Tue, Jul 1, 2008 at 3:45 PM, Boyd, Todd M. <[EMAIL PROTECTED]> wrote:

-----Original Message-----
From: Brian Dunning [mailto:[EMAIL PROTECTED]
Sent: Tuesday, July 01, 2008 3:27 PM
To: php-general@lists.php.net
Subject: [PHP] Splitting up long URLs

I have a web page that lists "most recent comments" in a left margin.
Sometimes people post long URLs, or even just really really long
words, that force that margin to display way too wide, screwing up the
page layout. Is there a way to make sure URLs or other text in a
string gets split up so this doesn't happen?

If there's a CSS solution that's better than a PHP solution I'll take
that too.   :-)

STFW: http://www.w3.org/TR/css3-text/#white-space

...doesn't say much in the article about whether or not it will break up
"words" rather than lines, but it's worth a shot.


Todd Boyd
Web Programmer

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to