正则表达式与 URL 不匹配


Regular expression not matching URL

我想运行这个PHP函数--

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

当我print $str时,它会给出错误:

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

拜托,我的正则表达式哪里错了?

你需要用分隔符包装你的正则表达式。

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

或者,不要使用正则表达式并使用 PHP 提供的内容。

parse_str($str, $params);
if (get_magic_quotes_gpc()) {
    $params = array_map('stripslashes', $params);
}
$params["startIndex"] = $sindex;
$str = http_build_query($params);