Connecting Tech Pros Worldwide Forums | Help | Site Map

Shortening URL's using PHP

Dariusz
Guest
 
Posts: n/a
#1: Jul 17 '05
I have a guestbook on one of my sites where the entires are formatted to be
x pixles in width. Now the text is input and output from a "blob" field
from a database... but when people input long URL's into their entries, the
formatting of the page is ruined.

Can anyone suggest a way for PHP to "read through" the text and substitute
a URL with a short text link, but still keeping the URL so it's clickable?
From my observations, the URL's may or may not (usually not) have the
prefix http://.

All I can think of is for PHP to count how many consecutive characters
there are before a "space" character, and substitute if it's over x
characters in length. But I don't know if this is possible.

Would prefer suggestions how to do it rather than actual code (I want some
challenge after all).

Thanks.

Dariusz

usenet@isotopeREEMOOVEmedia.com
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Shortening URL's using PHP


On Tue, 22 Mar 2005 08:58:07 GMT, ng@lycaus.plusYOURSHIT.com (Dariusz) wrote:
[color=blue]
>Can anyone suggest a way for PHP to "read through" the text and substitute
>a URL with a short text link, but still keeping the URL so it's clickable?[/color]

This doesn't address your consideration about shortening based on location of
spaces, but it shortens URLs that are more than 50 chars in length and appends
ellipses :

echo "<a href=\"" . $url . "\">" . substr($url, 0, 50);

if (strlen($url) > 50) {
echo "&nbsp;.&nbsp;.&nbsp;.";
}

echo "</a>";


HTH

Joe Webster
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Shortening URL's using PHP


Dariusz wrote:[color=blue]
> I have a guestbook on one of my sites where the entires are formatted to be
> x pixles in width. Now the text is input and output from a "blob" field
> from a database... but when people input long URL's into their entries, the
> formatting of the page is ruined.
>
> Can anyone suggest a way for PHP to "read through" the text and substitute
> a URL with a short text link, but still keeping the URL so it's clickable?
> From my observations, the URL's may or may not (usually not) have the
> prefix http://.
>
> All I can think of is for PHP to count how many consecutive characters
> there are before a "space" character, and substitute if it's over x
> characters in length. But I don't know if this is possible.
>
> Would prefer suggestions how to do it rather than actual code (I want some
> challenge after all).
>
> Thanks.
>
> Dariusz[/color]

A perfect use for a regular expression.

-Joe
Nik Coughin
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Shortening URL's using PHP


Dariusz wrote:[color=blue]
> I have a guestbook on one of my sites where the entires are formatted
> to be x pixles in width. Now the text is input and output from a
> "blob" field from a database... but when people input long URL's into
> their entries, the formatting of the page is ruined.
>
> Can anyone suggest a way for PHP to "read through" the text and
> substitute a URL with a short text link, but still keeping the URL so
> it's clickable? From my observations, the URL's may or may not
> (usually not) have the prefix http://.
>
> All I can think of is for PHP to count how many consecutive characters
> there are before a "space" character, and substitute if it's over x
> characters in length. But I don't know if this is possible.
>
> Would prefer suggestions how to do it rather than actual code (I want
> some challenge after all).
>
> Thanks.
>
> Dariusz[/color]

Have a look at this:

http://www.php.net/manual/en/function.preg-replace.php

Read the comment by syd_egan. Now, I know you didn't want actual code, but
there are several challenges here, one of them is that syd_egan's code
presumes that either the http:// or ftp:// schemes are present, so there is
your first challenge. Good luck.


Dariusz
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Shortening URL's using PHP


In article <1A20e.11960$1S4.1244345@news.xtra.co.nz>, "Nik Coughin" <nrkn!no-spam!@woosh.co.nz> wrote:[color=blue]
>Have a look at this:
>
>http://www.php.net/manual/en/function.preg-replace.php
>
>Read the comment by syd_egan. Now, I know you didn't want actual code, but
>there are several challenges here, one of them is that syd_egan's code
>presumes that either the http:// or ftp:// schemes are present, so there is
>your first challenge. Good luck.[/color]

Thank you to all who have replied to my problem with your pointers, I hope
I can work a solution myself ;-).

Dariusz
Closed Thread