0

I'm trying to evaluate user input from a form field as a Regex expression to match against a URL using PHP's preg_match() function.

The user would be expected to enter a Regex expression like ^\/test\/(.*)\/?$

This input would be passed as a string literal to preg_match as follows:

$url = $_SERVER['REQUEST_URI']; // /test/onetwothree/, for example
$regex = $_POST['regex__user_input']; // ^\/test\/(.*)\/?$
$match = preg_match($regex, $url); 

This regex should match the pattern but preg_match is not returning a result.

I think I need to add some delimiters or maybe use preg_quote()? I tried some variations but no dice.

1 Answer 1

1

you lost / at the begin and end of your regex.you can fill it in code or let user fill it.
preg_match return match return in reference parameters.as my code below

$url = $_SERVER['REQUEST_URI']; // /test/onetwothree/, for example
$regex = $_POST['regex__user_input']; // ^\/test\/(.*)\/?$
$regex = '/'.$regex.'/'; // you lost this
preg_match($regex, $url ,$matches); 
print_r($matches)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.