php preg_match(); - 未知修饰符“+”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5589807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preg_match(); - Unknown modifier '+'
提问by David Bradbury
Alright, so I'm currently working on parsing an RSS feed. I've gotten the data I need no problem, and all I have left is parsing the game title.
好的,所以我目前正在解析 RSS 提要。我已经得到了我不需要的数据,剩下的就是解析游戏标题。
Here is the code I currently have (ignore the sloppiness, it is just a proof of concept):
这是我目前拥有的代码(忽略草率,这只是一个概念证明):
<?php
$url = 'http://raptr.com/conexion/rss';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($result);
$lastgame = $xml->channel->item[0]->description;
preg_match('[a-zA-Z]+</a>.$', $lastgame, $match);
echo $match;
?>
Everything was working great, but then I started getting this error:
一切都很好,但后来我开始收到这个错误:
Warning: preg_match() [function.preg-match]:
Unknown modifier '+' in raptr.php on line 14
The only thing I have left is to strip out the closing anchor tag and the period, but I can't seem to figure out why it isn't liking the '+'. Any ideas?
我唯一剩下的就是去掉结束锚标记和句点,但我似乎无法弄清楚为什么它不喜欢“+”。有任何想法吗?
Edit: This should not be marked as a duplicate as it was asked two years before the other question.
编辑:这不应该被标记为重复,因为它是在另一个问题前两年被问到的。
回答by alex
You need to use delimiters with regexes in PHP. You can use the often used /
, but PHP lets you use any matching characters, so @
and #
are popular.
您需要在 PHP 中使用带有正则表达式的分隔符。您可以使用经常使用/
,但PHP让你使用任何匹配的字符,所以@
并#
很受欢迎。
If you are interpolating variables inside your regex, be sure to pass the delimiter you chose as the second argument to preg_quote()
.
如果您在正则表达式中插入变量,请确保将您选择作为第二个参数的分隔符传递给preg_quote()
.
回答by Khez
Try this code:
试试这个代码:
preg_match('/[a-zA-Z]+<\/a>.$/', $lastgame, $match);
print_r($match);
Using /
as a delimiter means you also need to escape it here, like so: <\/a>.
使用/
你还需要在这里逃吧,像这样的分隔符方式:<\ / A>。
UPDATE
更新
preg_match('/<a.*<a.*>(.*)</', $lastgame, $match);
echo'['.$match[1].']';
Might not be the best way...
可能不是最好的方法...
回答by David Winiecki
This happened to me because I put a variable in the regex and sometimes its string value included a slash. Solution: preg_quote.
这发生在我身上,因为我在正则表达式中放置了一个变量,有时它的字符串值包含一个斜杠。解决方案:preg_quote。
回答by Light
May be this will be usefull for u: ReGExp on-line editor
可能这对你有用: ReGExp 在线编辑器