[php] preg_match(); - Unknown modifier '+'

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.

This question is related to php xml parsing rss

The answer is


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


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


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


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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to parsing

Got a NumberFormatException while trying to parse a text file for objects Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) Python/Json:Expecting property name enclosed in double quotes Correctly Parsing JSON in Swift 3 How to get response as String using retrofit without using GSON or any other library in android UIButton action in table view cell "Expected BEGIN_OBJECT but was STRING at line 1 column 1" How to convert an XML file to nice pandas dataframe? How to extract multiple JSON objects from one file? How to sum digits of an integer in java?

Examples related to rss

Monitoring the Full Disclosure mailinglist UEFA/FIFA scores API How to parse an RSS feed using JavaScript? Best Way to read rss feed in .net Using C# preg_match(); - Unknown modifier '+' Best way to parse RSS/Atom feeds with PHP Parse RSS with jQuery How to apply CSS to iframe?