Preg_match 帮助查找计数

发布于 2024-09-07 01:06:31 字数 223 浏览 3 评论 0原文

大家好 我有一个字符串,

<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>

我需要得到 10000 作为答案..我如何使用 preg_match ???注意:这对于多次出现匹配非常重要,

提前致谢

HI all
i have a string

<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>

i need to get 10000 as answer .. how i use preg_match ??? Note: this is implortant that the multiple occurance of match

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2024-09-14 01:06:31

至少对于这种特殊情况,您可以使用 '/\(\d+\-\d+ of (\d+)\)/' 作为模式

它匹配这样的字符串 ({one-or-more-digits}-{one-or-more-digits} of {one-or-more-digits}) 并捕获最后一个 {一个或多个数字} 到一个组中(此处添加 {} 只是为了清楚起见..)。

$str = '<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>';
$matches = array();
if (preg_match('/\(\d+\-\d+ of (\d+)\)/', $str, $matches))
{
    print_r($matches);
}

prints:

Array
(
    [0] => (1-20 of 10000)
    [1] => 10000
)

因此,您要查找的 10000 个可以通过 $matches[1] 访问。


在评论后进行编辑:如果您多次出现 ({一个或多个数字}-{一个或多个数字},共 {一个或多个数字}),您可以使用 preg_match_all 来捕获它们。我不确定数字本身在没有它们出现的上下文的情况下有多大用处,但您可以这样做:

$str = '<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>';
$str .= "\n$str\n";
echo $str;
$matches = array();
preg_match_all('/\(\d+\-\d+ of (\d+)\)/', $str, $matches);
print_r($matches);

prints:

<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>
<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>
Array
(
    [0] => Array
        (
            [0] => (1-20 of 10000)
            [1] => (1-20 of 10000)
        )

    [1] => Array
        (
            [0] => 10000
            [1] => 10000
        )

)

同样,您要查找的内容将在 $matches[1],只是这一次它将是一个包含 1 个或多个实际值的数组。

At least for this particular case, you could use '/\(\d+\-\d+ of (\d+)\)/' as the pattern.

It matches strings like this ({one-or-more-digits}-{one-or-more-digits} of {one-or-more-digits}) and captures the last {one-or-more-digits} into a group ({}s added just for clarity's sake here..).

$str = '<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>';
$matches = array();
if (preg_match('/\(\d+\-\d+ of (\d+)\)/', $str, $matches))
{
    print_r($matches);
}

prints:

Array
(
    [0] => (1-20 of 10000)
    [1] => 10000
)

So, the 10000 you are looking for would be accessible at $matches[1].


Edit after your comment: If you have multiple occurrences of ({one-or-more-digits}-{one-or-more-digits} of {one-or-more-digits}), you can use preg_match_all to catch them all. I'm not sure how useful the numbers themselves are without the context in which they occur, but here's how you could do it:

$str = '<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>';
$str .= "\n$str\n";
echo $str;
$matches = array();
preg_match_all('/\(\d+\-\d+ of (\d+)\)/', $str, $matches);
print_r($matches);

prints:

<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>
<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>
Array
(
    [0] => Array
        (
            [0] => (1-20 of 10000)
            [1] => (1-20 of 10000)
        )

    [1] => Array
        (
            [0] => 10000
            [1] => 10000
        )

)

Again, what you're looking for would be in $matches[1], only this time it'll be an array containing 1 or more of the actual values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文