Jump to content

[SOLVED] Formatting a URL string


Canman2005

Recommended Posts

Hi all

 

I have a value defined like

 

$url = $_SERVER['HTTP_REFERER'];

print $url;

 

Which produces a url with a

 

did number which can be something like 55

 

and a

 

tid number which can be something like 3

 

a full example is

 

http://www.myweb.com/files/page.php?did=55&tid=3

 

I want to get php to format whatever is outputted and then remove everything before the did number and everything after it.

 

With the above, it would remove the first part

 

http://www.myweb.com/files/page.php?did=

 

and then the second part

 

&tid=3

 

and leave me with just

 

55

 

Can this be done? Its hard to explain what I mean but someone may understand me

 

Thanks in advance

 

Dave

Link to comment
Share on other sites

Try this:

 

<?php
$pattern = '/[^t=][0-9]+/';
preg_match($pattern, $url, $matches);
echo $matches[0]; //55
?>

If you have numbers in the file name or website or you have more variables in the url, you will need to add to $pattern variable. I'm sure someone could make a more bullet proof expression, but for your example, it should work.

 

 

Link to comment
Share on other sites

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.