Remove base URL from link in line

I have a line with a link to an image.

$image_link_raw = 'http://website.com/files/2012/10/image001.png'; 

Now I wanted to remove http://website.com and just get /files/2012/10/image001.png as:

 $image_link_raw = '/files/2012/10/image001.png'; 

Is there a way to do this in PHP?

+4
source share
3 answers

I think we just give answers then?

 <?php $image_link_raw = 'http://website.com/files/2012/10/image001.png'; $p=parse_url($image_link_raw); //print_r($p); echo $p['path']; 
+5
source
 $path = parse_url($url, PHP_URL_PATH); 

parse_url works great (and can provide you with other data like host, port, protocol, etc.)


 <?php $url = 'http://website.com/files/2012/10/image001.png'; echo 'URL Parts:' . PHP_EOL; var_dump(parse_url($url)); echo PHP_EOL . 'And specific to path:' . PHP_EOL; echo parse_url($url, PHP_URL_PATH); 

Result :

 URL Parts: array(3) { ["scheme"]=> string(4) "http" ["host"]=> string(11) "website.com" ["path"]=> string(27) "/files/2012/10/image001.png" } And specific to path: /files/2012/10/image001.png 
+10
source

I would look at parse_url in php, this will be the value ['path'] .

+6
source

Source: https://habr.com/ru/post/1438481/


All Articles