Solved

google of X results regex pattern for php function preg_match

Posted on 2009-03-31
4
436 Views
Last Modified: 2012-05-06
Im new to regex but not new to the php language. Im looking for a regex pattern that will capture the number of results from "ANY" of the following snippets... Also the content being searched is basically ALL the html from a google search so it needs to be precise and character limited so it doesnt pick up "of about" or "</b>" somewhere later in the content...

CODE:
preg_match($regexpattern,$content,$result);
print $result[1];

EXAMPLES:

CONTENT: Results <b>1</b> - <b>5</b> of <b>5</b> for
RESULT SHOULD BE: 5

CONTENT: Results <b>1</b> - <b>10</b> of about <b>15,000,000</b> for
RESULT SHOULD BE: 15000000

CONTENT: Results <b>1</b> - <b>8</b> of about <b>12,000</b> for
RESULT SHOULD BE: 12000

CONTENT: Results <b>1</b> - <b>8</b> of <b>987</b> for
RESULT SHOULD BE: 987
0
Comment
Question by:cimmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
obrienslalom earned 133 total points
ID: 24032934
Try something like the following.  Let me know if you have questions or your sample data is more complicated.
Results <b>[\d,.]*</b>\s*-\s*<b>[\d,.]*</b>\s*of[^<]*<b>([\d.,]*)</b> for

Open in new window

0
 
LVL 13

Expert Comment

by:marchent
ID: 24033557

preg_match('|of[^<]+<b>([\d,\s]+)</b> for|',$content,$result);
$result[1] = preg_replace('/\D/', '',$result[1]);
print $result[1];

Open in new window

0
 

Author Comment

by:cimmer
ID: 24034704
just so both of you guys know...  </b> has to be escaped such as <\/b>
obrienslalom's pattern worked great i ended up tweaking it a bit to the following...

preg_match('/Results 1\s*-\s*[\d]{1,2}\s*of[A-Za-z ]*([\d.,]{1,22}) for/si',$content,$results);
to simplify things I did a str_replace on $content to remove <b> tags before using the pattern above.
0
 

Author Closing Comment

by:cimmer
ID: 31565006
dont forget to escape / with a \/
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question