Reputation: 565
I am having below data.
Example 1
From : http://de.example.ch/biz/barbar-vintage-z%C3%BCrich
I want: /biz/barbar-vintage-z%C3%BCrich
And also if there is
http://www.example.ch/biz/barbar-vintage-z%C3%BCrich
Then I also want
/biz/barbar-vintage-z%C3%BCrich
Upvotes: 1
Views: 145
Reputation: 3868
Just try this:
<?php
$url = 'http://de.example.ch/biz/barbar-vintage-z%C3%BCrich';
echo preg_replace('~^https?://[^/]+~', '', $url);
?>
Upvotes: 0
Reputation: 174836
You could use preg_match
or preg_match_all
preg_match('~^https?://[^/]+\K.+~', $data, $matches);
Upvotes: 1
Reputation: 230
function getRelativePath($url)
{
$matches = array();
if (preg_match('#^(http://|https://)([^./]+\.)+[a-z]{2,3}(/.*)$#', $url, $matches) {
return $matches[3];
} else {
return false;
}
}
Upvotes: 1
Reputation: 786031
If you want to do it via regex then you can use:
$s = 'http://de.example.ch/biz/barbar-vintage-z%C3%BCrich';
echo preg_replace('~^https?://[^/]+~', '', $s);
//=> /biz/barbar-vintage-z%C3%BCrich
Otherwise as the comments says parse_url
function also let you have this value.
Upvotes: 3