SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: regexp question

  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regexp question

    Hi

    I am trying to wrap my head around this simple regexp but can't get it done.

    On each page of my site, I will need to retrieve the url of the current page.
    I am going to use $_SERVER['PHP_SELF'] to do so.

    Each url will be built like this http://www.[variousChars]&l=[something].
    which, in real life, might be something like this:

    http://www.mysite.com/blah.php?t=21&p=2&l=fr

    What I'd like to do is to get the whole url except the '&l=[something]'.

    Each url will end with the following chars: '&l=' then a few options might come: 'fr', 'en' etc... which is not relevant anyways as I want to retrieve what is before before these chars (the whole url before '&l=').

    Can anyone show me the light?

    thanks in advance for your help

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    If "&l=whatever" is *always* the last part of the URL you can just use:

    PHP Code:
    $url 'http://www.mysite.com/blah.php?t=21&p=2&l=fr';
    $new_url substr($url0strrpos($url'&'));
    echo 
    $new_url
    This gets the position of the last occurrence of '&' and returns the text from the beginning up to this position.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Leuven, Belgium
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can try:
    $new_url = preg_replace('#&(amp;)?l=[^&\'"]*#', '', $oldurl);

    'try before you buy' - I often don't get regexes on my first shot and haven't tried this one out.

  4. #4
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your replies.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •