preg_match(); - Unknown modifier '+' [duplicate]
Asked Answered
E

4

59

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?

Edit: This should not be marked as a duplicate as it was asked two years before the other question.

Evelyn answered 8/4, 2011 at 2:35 Comment(7)
Inspecting the rss feed, you don't want to use the $ there.Clod
When I remove the $ it only matches "Conexion", I appreciate the help though!Evelyn
What did you intend to match? All links by any chance? Then try preg_match_all. Otherwise try an RSS parser.Clod
I just want the most recent game played. It is always the text within the second link. I wish they organized the feed better.Evelyn
Use preg_match_all and get the second entry.Clod
@Clod - It sort of works, but for some reason it is returning an array inside of an array - Array ( [0] => Array ( [0] => Conexion [1] => Minecraft ) ) Evelyn
Yes, that's the difference to the normal preg_match which would just return one result.Clod
D
72

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.

Further Reading.

If you are interpolating variables inside your regex, be sure to pass the delimiter you chose as the second argument to preg_quote().

Dodecagon answered 8/4, 2011 at 2:38 Comment(1)
Well, that was a surprise. string preg_quote ( string $str [, string $delimiter = NULL ] ) -> function signature forces nothing as default, thus kicked me in the balls when least expected. Default should be /.Triggerfish
D
30

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>.

UPDATE

preg_match('/<a.*<a.*>(.*)</', $lastgame, $match);
echo'['.$match[1].']';

Might not be the best way...

Dd answered 8/4, 2011 at 2:38 Comment(6)
print_r($match); returns "Array ( )"I'm pretty sure my expression is correct.Evelyn
I don't know what you tried to say with your comment...Dd
@Conexion, checked your other comments, what is it you are trying to pull from that rss feed into your match ? The games ?Dd
Yup, just trying to pull the most recent game :)Evelyn
It works, but the problem is, the feed doesn't always being with "played some". Sorry I'm being difficult!Evelyn
Updated my code yet again, considered this time you always have <a>USER</a> <a>Game</a> in that order.Dd
G
6

This happened to me because I put a variable in the regex and sometimes its string value included a slash. Solution: preg_quote.

Gasbag answered 20/11, 2014 at 0:3 Comment(1)
Thank you for letting me know about preg_quote - very useful function!Rivalry
T
1

May be this will be usefull for u: ReGExp on-line editor

Tressatressia answered 8/4, 2011 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.