preg_match(); - Unknown modifier '+'

PhpXmlParsingRss

Php Problem Overview


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.

Php Solutions


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

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

Solution 2 - Php

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

UPDATE

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

Might not be the best way...

Solution 3 - Php

This happened to me because I put a variable in the regex and sometimes its string value included a slash. Solution: http://php.net/manual/en/function.preg-quote.php">preg_quote</a>;.

Solution 4 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid BradburyView Question on Stackoverflow
Solution 1 - PhpalexView Answer on Stackoverflow
Solution 2 - PhpKhezView Answer on Stackoverflow
Solution 3 - PhpDavid WinieckiView Answer on Stackoverflow
Solution 4 - PhpLightView Answer on Stackoverflow