0

I am using the Following code

<?php
$url = 'http://www.ewwsdf.org/012Q/rhod-05.php?arg=value#anchor';
$parse = parse_url($url);
$lnk= "http://".$parse['host'].$parse['path'];
echo $lnk;
?>

This is giving me the output as

http://www.ewwsdf.org/012Q/rhod-05.php

How can i modify the code so that i get the output as

http://www.ewwsdf.org/012Q/

Just need the Directory name without the file name

( I actually need the link so that i can link up the images which are on the pages, By appending the link behind the image Eg http://www.ewwsdf.org/012Q/hi.jpg )

1

4 Answers 4

4

Just need the Directory name without the file name

Then use dirname(), eg

$lnk= "http://".$parse['host'].dirname($parse['path']);
Sign up to request clarification or add additional context in comments.

1 Comment

@Adi To be fair, @nikic gave this same answer first (by 13 seconds)
3
$lnk = "http://".$parse['host'].dirname($parse['path']).'/';

dirname returns the parent directory's path.

Comments

3

use pathinfo() instead, it shows relevant info already parsed

3 Comments

+1. $parse = pathinfo($url); echo $parse['dirname']; is exactly what you want.
@mfonda: No, it isn't he would need to first parse_url and then pathinfo the 'path'. It is better in this case to use just dirname.
@nikic: why would he need to first use parse_url()? pathinfo() can take a full URL as an argument
1

you could do something like this:

$sections = explode("/", $_SERVER['REQUEST_URI']);
$folder = $sections[1];
$url = "http://www.ewwsdf.org/".$folder."/";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.