Woddles
Woddles

Reputation: 51

Regex preg_match() issues

I cannot get the following regex to work properly:

preg_match('Currently: ([0-9\.km,]+)', $data, $matches)   

The information inside of $data is: 'What it is Currently: 52,523' (along with about 30 lines of html).

I keep getting the following error: Warning: preg_match(): Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\test\test.php on line 49

Note: Line 49 contains the preg_match that I posted above.

Edit: Apologies, i forgot to add in the matches parameter.

Upvotes: 0

Views: 291

Answers (3)

Polyana Fontes
Polyana Fontes

Reputation: 3216

the preg_match function requires a delimiter in your pattern.

preg_match('/Currently: ([0-9\.km,]+)/', $data, $matches)   

edit:

as described in the comments

@JoséRobertoAraújoJúnior curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); If that is what you mean, yes I have set it. If i echo $data, it displays the webpage, which means it still contains all the html tags.. I'm not sure what to do from here, to use that data in my preg_

Then it's possible that $data contains white-spaces between: Currently: and (...), so this should be considered in your pattern by adding \s+ instead of a normal white-space, \s will match any white space character as described in the PCRE regex syntax documentation page.

preg_match('/Currently:\s+([0-9\.km,]+)/', $data, $matches) 

Live test: http://codepad.viper-7.com/xJNisE

Upvotes: 5

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You have to use delimiter like this edit

$data = 'What it is Currently: 52,523';
preg_match('&Currently: ([0-9\.km,]+)&', $data, $match);
print_r($match);

working example http://codepad.viper-7.com/QGOoXT

Upvotes: 1

Naryl
Naryl

Reputation: 1888

you need to add the same character at the begining and end of the pattern:

preg_match('/Currently: ([0-9\.km,]+)/', $data)   

This character can't appear inside the pattern unless you escape it, for example:

preg_match('/<example><\/example>/', $xml)  

You can use other characters as delimiters, one of the most used beside / is #

Upvotes: 1

Related Questions