user1045052
user1045052

Reputation: 23

Regular expression not matching URL

I want to run this PHP function --

$querystring_arr='maxResults=50&startIndex=50&sort=date';
$str=preg_replace("(&startIndex=)?[0-9]*(&)?","&startIndex=".$sindex."&",$querystring_arr);

When I print $str it gives the error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '\' in C:\xampp\htdocs\myapp\paginator.class.php on line 112

Please, where is my regular expression wrong?

Upvotes: 1

Views: 143

Answers (1)

alex
alex

Reputation: 490657

You need to wrap you regex with delimiters.

preg_replace("/(&startIndex=)?\d*&?/","&startIndex=".$sindex."&",$arr);

Alternatively, don't use a regex and use what PHP provides.

parse_str($str, $params);

if (get_magic_quotes_gpc()) {
    $params = array_map('stripslashes', $params);
}

$params["startIndex"] = $sindex;

$str = http_build_query($params);

Upvotes: 2

Related Questions