Jason
Jason

Reputation: 15378

Cherry pick numbers from a paragraph only if there is a certain word on that line

From the following prose, I want to extract a list of numbers from any -line- that contains the letters "HTML". html could be upper case or lower case.

So here's the psuedo code: text = getline() if text contains html match any numbers from text return array of matches

Any ideas how to do this in REG Ex?

===============

HTML email is still … a great marketing tool if used properly. The key is to test, test,
 test to see if your subscribers prefer 5 it over text based email. If you are unsure your
 subscribers can read HTML email, then offer both text-based email and HTML 7 email, to 
cater to both audiences.

In my Part $254,000 of this article, I will discuss “How to create and send an HTML email 
form” to increase the interactivity of your subscribers and boost the response rate in your
 email marketing campaigns. retro 50's 

=============

Upvotes: 0

Views: 156

Answers (3)

Toto
Toto

Reputation: 91488

First check if there is html in the string, then match all digits:

if (preg_match("/html/i", $input)) {
    preg_match_all("/\b(\d+)\b/", $input, $m);
}
print_r($m);

Upvotes: 1

orlp
orlp

Reputation: 117761

^((\d+)|.)*(HTML|html)((\d+)|.)*$

Getting the capture groups right might be a little tricky, but just mess around a little.

Upvotes: 0

Benubird
Benubird

Reputation: 19507

Sample solution:

$line=strtolower($line);
$x = preg_match("/html.*(\d+)/",$line,$match) || preg_match("/(\d+).*html/",$line,$match);
if($x)echo "number in line: ".$match[1];

Assumes only one number in line. Definitely not the best way to do it, but you really should learn regex yourself - it's not that hard.

See also http://www.regular-expressions.info/

Upvotes: 0

Related Questions