错误的结果regexp

时间:2011-11-03 07:06:49

标签: php regex

这是我的字符串:

/index.php?option=com_podstrony&id=1

我希望从中得到“com_podstrony”

这是我的正则表达式:/^\/*index.php\?option=(.*)+&.*$/ 但结果是错误的:

option=com_podstrony&id=1

我正在使用PHP preg_replace

任何人都可以帮助我?

2 个答案:

答案 0 :(得分:5)

您不需要使用正则表达式...

parse_str(parse_url($url, PHP_URL_QUERY), $get);

echo get_magic_quotes_gpc() ? stripslashes($get['option']) : $get['option'];

CodePad

但如果你想出于某种原因使用正则表达式......

preg_match('/^\/index\.php\?option=(?P<option>[^&;]+).*$/', $url, $matches);

echo $matches['option'];

CodePad

答案 1 :(得分:0)

首先,这个正则表达式怎么样?

@^/*index.php\?option=(.+)\&.@

我删除了“行尾”并将/分隔符替换为@

其次,您确定需要使用regexp解析URL吗?通常的做法是仅解析index.php部分并将其余部分留给您的webapp($_GET参数)。

相关问题