Jump to content

extract the middle part of this url


Go to solution Solved by 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

  • Solution

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.

Edited by Christian F.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.