0

I have tried the following code:

preg_match_all("/'<span> '+
'(.*?) Reviews/s", $data, $matches);

I am trying to get the number of reviews, here is a sample of the $data:

'<span> '+
'319 Reviews </span> '+
'</div> '+

My script is returning a $matches array empty - any ideas?

2 Answers 2

2

You can try this pattern:

/'<span> '\+\s*'\K[0-9]+(?= Reviews)/

\s is a character class for all spaces characters (including newlines)

\K means keep out and remove all on the left from match result

(?=...) is a lookahead that checks what is following without includes it in the match result.

Sign up to request clarification or add additional context in comments.

2 Comments

What's about a new line?
@zerkms: the newline is matched with \s* and is optional, since it is not an important element of the javascript syntax here. (it is outside the string)
0

You could use the below regex which uses Dotall(s) modifier,

/'<span> '\+.\'\K\d+(?= Reviews)/s

DEMO

OR

you could use a lookbehind with s modifier,

/(?<='<span> '\+.\')\d+(?= Reviews)/s

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.