Jump to content

Truncating a url?


sx

Recommended Posts

I tried to find this and I tried to get it to work with truncate but no luck.  What I want to do is remove some of a url.

 

The host and lenth of the length may vary greatly too depending on who is posting the link. The host can be anyone.

 

Say I have this url

http://rapidshare.com/files/9839432/asdf/sadf/asd/fasd/fTiger_Army.rar

 

I want it to change to

http://rapidshare.com/.../fTiger_Army.rar

 

Or this one

http://i11.tinypic.com/hello/485awkj.jpg

 

It should show up as

http://i11.tinypic.com/.../485awkj.jpg

Link to comment
https://forums.phpfreaks.com/topic/43502-truncating-a-url/
Share on other sites

Ok.  That was kewl thank you. I got it to seperate the host from the rest. That was kewl.  Now What I want to do is start counting from the RIGHT back about 20 characters and then remove everything before that point and leave everything after it..

So this

/files/9839432/asdf/sadf/asd/fasd/fTiger_Army.rar

Should become

...sd/fTiger_Army.rar

 

Here is what I did so far only it doesn't work right.  It removes everything AFTER that point.

 

function truncate ($string, $max = 20, $rep = '') {
   $leave = $max - strlen ($rep);
   return substr_replace($string, $rep, $leave);
}
$url = 'http://rapidshare.com/files/9839432/asdf/sadf/asd/fasd/fTiger_Army.rar';
print_r(parse_url($url));
$home =  parse_url($url, PHP_URL_PATH);
$home =  truncate ($home, '-6', '...'  );
echo "<br><br><br>";
echo "$home";

Link to comment
https://forums.phpfreaks.com/topic/43502-truncating-a-url/#findComment-211317
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.