Jump to content

extract the middle part of this url


chetankchandak

Recommended Posts

From  http://www.shopclues.com/1862-hey-dude-footwear.html%3Fref%3D20 I want the output to display 

shopclues.com/1862-hey-dude-footwear.html

 

data after . and before %

I think i am missing something in the preg_match

 

Plz help

<?php
// get host name from URL
preg_match('@^(?:http://)?([^%]+)@i',
    "http://www.shopclues.com/1862-hey-dude-footwear.html%3Fref%3D20", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
 ?>
Link to comment
https://forums.phpfreaks.com/topic/278979-extract-the-middle-part-of-this-url/
Share on other sites

thanks but solved it from other way 

<?php
// get host name from URL
preg_match('@^(?:http://) ?([^%]+)@i',
    "http://www.shopclues.com/1862-hey-dude-footwear.html%3Fref%3D20", $matches);
$host = $matches[1];
echo "<br>";
$site_url=str_replace("www.","",$host);
echo $site_url;
 ?>

You really should be using the parse_url function, as that RegExp of yours allow for some non-desirable strings to slip through. Strings that might not even be an URI.

Not to mention that it's a whole lot easier to read, and thus maintain.

try using parse_url() php function

You really should be using the parse_url function.

Some guidance for the OP about using parse_url() for his particular needs wouldn't go amiss, guys; especially since parse_url() would only go a small way towards resolving the question posed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.