Jump to content

Dividing a link function


etrader

Recommended Posts

In a script links are captured from the internet by $link, then display it by echo $link

 

The links have the general format of http://domain.com/something/filename.html

 

I am looking for a simple way to display capture some elements from $link and display "domain.com" and "filename"

 

In other words, I need a code to remove other parts of the link provided by $link

 

:confused::D

Link to comment
Share on other sites

This is quite tricky to do because of the large number of domain options available and also the number of subdirectories it could go into, logic could be something like this

-explode based on the "/"

-then search the array for values with a "." and put them into new array

-sum the new array and use the first and last values just incase some subdirectory names have "."

-then for the filename i would explode "." on the last value and use the first value in that array

Link to comment
Share on other sites

I would use PHPs parse_url() function:

<?php
print_r(parse_url("http://domain.com/something/filename.html"));
/*
Array
(
[scheme] => http
[host] => domain.com
[path] => /something/filename.html
)
*/

 

Now it's easy to pick up stuff from that array. And parsing the path for the information you need isn't that difficult.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.