Lalit Sharma
Lalit Sharma

Reputation: 565

Remove any kind of url from string in php?

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

Answers (4)

Priyank
Priyank

Reputation: 3868

Just try this:

<?php 
$url = 'http://de.example.ch/biz/barbar-vintage-z%C3%BCrich';
echo preg_replace('~^https?://[^/]+~', '', $url);
?>

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174836

You could use preg_match or preg_match_all

preg_match('~^https?://[^/]+\K.+~', $data, $matches);

DEMO

Upvotes: 1

Mohamed Alkaduhimi
Mohamed Alkaduhimi

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

anubhava
anubhava

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

Related Questions