John R Perry
John R Perry

Reputation: 4192

How to return the full URL before the hash (using php)?

The question basically covers it. I'm sure this question has been asked, but I can't seem to find it.

Just to be clear, I have this url

https://www.example.com/page.php#!/more-and-more/text/that-needs-to-go/away

and I'd like to return with this url

https://www.example.com/page.php

I think I'm supposed to use substr but alas, I am here.

Upvotes: 0

Views: 215

Answers (3)

Andre Santos
Andre Santos

Reputation: 11

$url = 'https://www.example.com/page.php#!/more-and-more/text/that-needs-to-go/away'; print_r(parse_url($url));

or

$pattern = '/.*page.php/';

preg_match($pattern, $url, $matches);

print_r($matches);

Upvotes: 0

Christian
Christian

Reputation: 799

if you use the same tag "#", you can do the following:

$url = 'https://www.example.com/page.php#!/more-and-more/text/that-needs-to-go/away';
echo strstr($url, '#', true);

Upvotes: 0

Meenesh Jain
Meenesh Jain

Reputation: 2528

If you are always sure that you will have a # in your url then

explode() - http://php.net/manual/en/function.explode.php

here what you need

 $url = 'https://www.example.com/page.php#!/more-and-more/text/that-needs-to-go/away';
 $temp = explode("#",$url);
 $url = $temp[0];
 echo $url;

// output - https://www.example.com/page.php

i think this may be helpful

Other than that you can do this

substr() - http://php.net/manual/en/function.substr.php

strpos() - http://php.net/manual/en/function.strpos.php

 $url = 'https://www.example.com/page.php#!/more-and-more/text/that-needs-to-go/away';

 echo $url=substr($url,0,strpos($url,"#"));

 // output - https://www.example.com/page.php

// this will also work

Upvotes: 1

Related Questions