chetankchandak Posted June 10, 2013 Share Posted June 10, 2013 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/278979-extract-the-middle-part-of-this-url/ Share on other sites More sharing options...
PravinS Posted June 10, 2013 Share Posted June 10, 2013 try using parse_url() php function refer: http://php.net/manual/en/function.parse-url.php Quote Link to comment https://forums.phpfreaks.com/topic/278979-extract-the-middle-part-of-this-url/#findComment-1435096 Share on other sites More sharing options...
Solution chetankchandak Posted June 10, 2013 Author Solution Share Posted June 10, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/278979-extract-the-middle-part-of-this-url/#findComment-1435098 Share on other sites More sharing options...
Christian F. Posted June 10, 2013 Share Posted June 10, 2013 (edited) 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. Edited June 10, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/278979-extract-the-middle-part-of-this-url/#findComment-1435099 Share on other sites More sharing options...
salathe Posted June 10, 2013 Share Posted June 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278979-extract-the-middle-part-of-this-url/#findComment-1435118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.