Shortening URLs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SpaceManSpiff
Forum Newbie
Posts: 6
Joined: Sat Apr 27, 2002 8:55 pm

Shortening URLs

Post by SpaceManSpiff »

How can I have PHP cut off anything after the 50th character of a variable?

Say I have...

Code: Select all

$url = "http://dir.yahoo.com/Business_and_Economy/Shopping_and_Services/Photography/Photojournalism/";
How can I get the URL shortened into like the first 50 characters so that I can do something like..

Code: Select all

<a href="http://dir.yahoo.com/Business_and_Economy/Shopping_and_Services/Photography/Photojournalism/">http://dir.yahoo.com/Business_and_Economy/Shopp...</a>

------

<a href="$url">$urlshort</a>
Gamefreak
Forum Newbie
Posts: 1
Joined: Sat Apr 27, 2002 9:05 pm

Post by Gamefreak »

Ok, here's how you do it:

Code: Select all

$full_url = "this.really/long/url/";

if (strlen($full_url)>=50) &#123;
$short_url = substr(%full_url, 0, 50);
$short_url .= "...";
&#125;
Your url will have the "..." on the end only if it is 50 characters or longer. Hope this helps you!
SpaceManSpiff
Forum Newbie
Posts: 6
Joined: Sat Apr 27, 2002 8:55 pm

Post by SpaceManSpiff »

That's exactly what I wanted. Thanks! :)
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post by learning_php_mysql »

is their anyway to get the url directly from the addressbar?
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

yea:

Code: Select all

echo $_SERVER&#1111;"HTTP_HOST"]$SERVER&#1111;"REQUEST_URI"];
:)
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post by learning_php_mysql »

sweet, is there a way to like take that then format it then plug something else into the address bar?
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

yea,

Code: Select all

$currentURL = $SERVER&#1111;"HTTP_HOST"]$SERVER&#1111;"REQUEST_URI"];
//code to alter the currentURL - set variable $newURL
header("Location: $newURL");
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post by learning_php_mysql »

i meant is tehre a way to make the url look different?
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

what do u mean exactly? what URL are u talking about?
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post by learning_php_mysql »

lets say my website is at http://www.site.com

i have an admin cp and lets say its files are in http://www.site.com/acp/

is there a way to make the url in the address bar look like

http://www.site.com...

?

thanks
Post Reply