Forum Moderators: coopster

Message Too Old, No Replies

Find and Trim a URL?

         

steww

9:47 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi there, this should be fairly simple - hopefully :-)

We have wordpress site and within a functions php file we are grabbing a url from a post page.

All we want to do is grab and store everything from the url AFTER the domain name, for example our url is "htttp:www.mysite.com/product?123"

We want to grab just "/product123" and store it as a variable to use later.

Any ideas?



---
Our original code was as follows but doesn't seem to work:


/*Finds first slash */

$mystring = $Product_URL;

$findme = '/';

$pos = strpos($mystring, $findme, 7);

/*Returns everything after the domain */

$pathend = substr($mystring,$pos,300);

Matthew1980

10:10 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there stew,

There is probably a regex for that somewhere, but I don't know enough to show an example, though I expect someone will provide an example, but I shall offer this instead:-
$Product_URL = "htttp:www.mysite.com/product?123";

$URLGot = str_replace("?","",strrchr("htttp:www.mysite.com/product?123", "/"));

echo $URLGot;

would give -> /product123

That will do it!

Cheers,
MRb

steww

10:36 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi MRB many thanks for this, apologies but have just realised the URL can contain slashes after the http (e.g. http//:www.mysite.com/product?123)
so not sure if your soloution would still work?

Cheers

Matthew1980

10:51 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Steww,

Ah, yes, that dawned on me a moment ago when I re read the post to see if you were using your own format URL, again, regex isn't my strong suite, so I do this instead:-

//this would be the normal URL spaces are for the benefit of this forum :)
$url = "h tt p:// www.mysite.com/product?123";

//strip the :// from it
$url = str_replace("://","",$url);

//now strip the other slash & remove the querystring
echo str_replace("?","",strrchr($url, "/"));

this will also give you "/product123"

Hope that's what you wanted this time ;)

Cheers,
MRb

steww

11:07 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi MRb, excellent thanks for getting around that, looks like just what we need, will give it a try! :-) thanks

steww

2:48 am on Jul 10, 2010 (gmt 0)

10+ Year Member



Quick update, worked a treat, I needed to keep the "?" in the url so changed the last bit to:

echo strrchr($url, "/");

Many Thanks for that,

Cheers

Matthew1980

9:30 am on Jul 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there steww,

As I said though, there is more than likely a more elegant way of achieving that, but it works, glad your all sorted out now.

Happy coding!

Cheers,
MRb

steww

11:17 am on Aug 4, 2010 (gmt 0)

10+ Year Member



Hi there, update!

This soloution worked great, but have come accross an issue if the url has more than one directory,

Therefore it works with:
http//:www.mysite.com/product?123

But not with:
http//:www.mysite.com/category/product?123

I need to extract "/category/product?123"

But with above soloution I am only getting the last part:

"/product?123"


Any ideas of how I can get around this?

bedlam

3:23 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think these solutions are a bit too complicated.

For simplicity, it's hard to beat using the contents of the $_SERVER [php.net] variable:

$URL_Got = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];

There's also a helpful PHP function you could use: parse_url() [php.net]. With that, you'd do something like this:

$url_parts = parse_url($Product_URL);
$URL_Got = $url_parts['path'] . '?' . $url_parts['query'];


-- b

steww

4:26 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Bedlam - brilliant!

I tried:

$url_parts = parse_url($Product_URL);
$URL_Got = $url_parts['path'] . '?' . $url_parts['query'];

And it worked first time for both types of URLS.

Many Thanks :-)

bedlam

4:47 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NP.

Of the two solutions I suggested, that's actually the better one anyhow, since you're probably using mod_rewrite $_SERVER['SCRIPT_NAME'] could return 'index.php' instead of the Wordpress url, and $_SERVER['QUERY_STRING'] might have more stuff in it than you're anticipating.

Incidentally, I haven't looked at the code samples above in the thread in detail, but since you know your own domain, wouldn't this have worked?

$URL_Got = str_replace('http://www.example.com', '', $Product_URL)


There shouldn't be any issue with www or non-www urls since you should be ensuring that the one is redirected to another in any case. If you needed to take account of subdomains in general, you could do that with preg_replace() [php.net]:

// Match occurrences of 'example.com'
// preceded by 'http://' and zero or
// more occurrences of an alphanumeric
// string followed by a dot:
$pattern = '/http:\/\/([a-z0-9]+\.)*example.com/i';
// Replace any of those matches with an empty string:
$URL_Got = preg_replace($pattern, '', $Product_URL);


-- b

Matthew1980

5:40 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Steww,

Glad to see a solution was found anyway, I was trying some regex versions to see if I could get it to work, but as this method doesn't even use regex - cool!

Cheers,
MRb