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?


// 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');
}

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 :slight_smile:

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.

:slight_smile:

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. :frowning:


$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.