只有当该行中有某个单词时,才会从段落中选择Cherry编号

时间:2011-02-04 10:53:20

标签: php regex preg-match

从下面的散文中,我想从任意一行中提取一个包含字母“HTML”的数字列表。 html可以是大写或小写。

所以这是psuedo代码: text = getline() 如果文本包含html    匹配文本中的任何数字    返回匹配数组

如何在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 

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

3 个答案:

答案 0 :(得分:1)

首先检查字符串中是否有html,然后匹配所有数字:

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

答案 1 :(得分:0)

样本解决方案:

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

假设只有一个数字。绝对不是最好的方法,但你真的应该自己学习正则表达式 - 这并不难。

另见http://www.regular-expressions.info/

答案 2 :(得分:0)

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

使捕获组正确可能有点棘手,但只是乱七八糟。