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
https://forums.phpfreaks.com/topic/39719-solved-formatting-a-url-string/
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.

 

 

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.