[UPHPU] php equivalent of Request.ServerVariables
Daniel Stephenson
dan.stephenson at gmail.com
Fri Sep 9 13:48:24 MDT 2005
On 9/9/05, Sheri Bigelow <design5279 at gmail.com> wrote:
>
> On 9/9/05, Wade Preston Shearer <lists at wadeshearer.com> wrote:
> >
> > > Is there a way to get the current url using php or javascript? Like
> > > Request.ServerVariables("URL") in asp?
> > >
> > > What I want to do is take a url like this:
> > >
> > > http://www.ashley.oviatt.whatever.com/
> > >
> > > and get the ashley and oviatt out of it to put on the current page.
> > > I've seen code do this, but can't seem to find it anywhere.
> >
> > Yes, in the $_SERVER global array:
> >
> > $_SERVER['HTTP_HOST']
>
> And, you could used something like this to remove the extra stuff from the
> url that you don't want.
>
> <?php
> $url = $_SERVER['HTTP_HOST'];
> // uncomment line below to test your url
> // $url = "http://www.ashley.oviatt.whatever.com/";
> $stufftoremove = array("http://", "www.", ".whatever", ".com/", ".com");
> echo str_replace($stufftoremove, "", $url);
> ?>
>
> Of course, you would have to know ahead of time that you want to remove "
> whatever.com <http://whatever.com> <http://whatever.com>" in this case.
> For "
> http://www.ashley.oviatt.whatever.com/", the example above would return "
> ashley.oviatt".
>
>
Another way you can use to extract the information you need is through some
type of regular expression (e.g.
<?
$url = "http://www.ashley.oviatt.whatever.com/"; // $_SERVER['HTTP_HOST']
$matches = array();
preg_match("/.*www\.(.*)\.com/", $url, $matches);
foreach ($matches as $match) { echo "Match: $match<br>"; }
?>
// Will print Matches: ashley.oviatt.whatever
)
You can widdle this down even further (there are million ways to write this
regular expression), or just do a split on the . inside this string to
capture the part that you really want from this.
-Daniel-
More information about the UPHPU
mailing list