SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegEx match anything after a word. Can't figure out why this doesn't work.

    I got a small 3 page website. I'm using url rewrite to avoid query strings and it works well, but my contact page needs variables in the url to return any errors. So I am now trying to use regex to capture errors & create variables to echo data back into the form & show errors.
    I have done this code below to check url & include the right page which works fine, except when I use contact-us/whatever-I-want-to-capture. Then I get a warning: Warning: include(whatever-I-want-to-capture.php) [function.include]: failed to open stream: No such file or directory
    What's going wrong here?

    PHP Code:
    // include the page according to the url
    $url $_SERVER['REQUEST_URI'];
    $url str_replace (SITE,'',$url);
    $contact preg_match('/contact-us.*/',$url);
    if (!
    $url){
        include(
    'home.php');
    }
    if (
    $url $contact){
        include(
    'contact-us.php');
    }

    else { include(
    $url.'.php');


  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    elseif doh!

    Edit: Ah no it wasn't elseif that solved it. It works only if I don't use a '/' in the url. Good enough, it'll work

  3. #3
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,332
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    You have fallen into the Assignment is not Equality trap.
    When assigning a value use =
    When testing equality use == (or === in some languages, including PHP)
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ParkinT View Post
    You have fallen into the Assignment is not Equality trap.
    When assigning a value use =
    When testing equality use == (or === in some languages, including PHP)
    Hi, thanks for that. I did try using ==, but it still returned the same error. It seems to only work as long as there are no forward slashes in the url.


  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I got the url to match contact-us-whatever%20I want but it will only work if I use a - character to separate each capture. Not sure why.

    So now I want to capture the data, but I think I'm barking up the wrong tree here. This works so far, but what if the user posts a - character in their email address or their message? That will knacker my contact us script.
    PHP Code:
    $url $_SERVER['REQUEST_URI'];
    $url str_replace (SITE,'',$url);
    preg_match('/^contact-us-(?P<fname>[a-zA-Z]+)-(?P<sname>[a-zA-Z]+)-(?P<message>.*+)/',$url,$matches);

    print_r ($matches); 
    // contact-us-Joe-Bloggs-anything I want here!!)(*&^

    // returns 
    // Array ( [0] => contact-us-Joe-Bloggs-anything%20I%20want%20here!!)(*&%5E [fname] => Joe [1] => Joe [sname] => Bloggs [2] => Bloggs [message] => anything%20I%20want%20here!!)(*&%5E [3] => anything%20I%20want%20here!!)(*&%5E ) 
    But for some reason if I add in -(?P<email>.*+) before the message, the script doesn't work.


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
  •