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
https://forums.phpfreaks.com/topic/222796-dividing-a-link-function/
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

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.

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.