preg_match(); -未知修饰符"+" [英] preg_match(); - Unknown modifier '+'
问题描述
好的,所以我目前正在解析RSS feed.我已经获得了毫无疑问的数据,剩下的只是解析游戏标题.
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.
这是我当前拥有的代码(忽略草率,这只是概念的证明):
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?
不应将其标记为重复,因为它是在另一个问题问了两年之前提出的.
This should not be marked as a duplicate as it was asked two years before the other question.
推荐答案
您需要在PHP中对正则表达式使用定界符.您可以使用常用的/
,但是PHP允许您使用任何匹配的字符,因此@
和#
很流行.
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.
如果您要在正则表达式中插入变量,请确保将您选择的定界符作为第二个参数传递给 preg_quote()
.
If you are interpolating variables inside your regex, be sure to pass the delimiter you chose as the second argument to preg_quote()
.
这篇关于preg_match(); -未知修饰符"+"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!