使用Preg_Match获取单个数字

时间:2012-02-27 03:01:29

标签: php regex

    <span class="nowrap">
        <span class="use_sprites stars4 stars4i5" title="5 start hotel">&nbsp;
    </span>

我需要获取标题之间的单个数字5。

这是我的尝试:

preg_match_all('#<span class="nowrap">\s*(.*?)\s*<\/span>#s', $htmlOfPage, $rate);

以上代码提取以下内容:

<span class="use_sprites stars4 stars4i5" title="5 start hotel">&nbsp;

问题:

  • 起点:<span class="nowrap">
  • 结束点:</span>
  • 目标部分:title="5 start hotel"
  • 目标价值:the single number in between title

我需要从起点开始并忽略除title =“...”之外的所有内容,并在标题中抓取单个数字。到目前为止,我们已经到了终点。

如何帮助您完成上述步骤?

1 个答案:

答案 0 :(得分:2)

$str = '<span class="nowrap">
        <span class="use_sprites stars4 stars4i5" title="5 start hotel">&nbsp;
    </span>';

preg_match('/<span class="nowrap">.+? title="([0-9]){1}/sm', $str, $match);

print_r($match);

输出:

Array
(
    [0] => <span class="nowrap">
        <span class="use_sprites stars4 stars4i5" title="5
    [1] => 5
)

确保查看匹配的源代码。由于span标记

,它不会显示在浏览器窗口中

这应该与你要找的号码相匹配

相关问题