用于新行的PHP preg_match_all

时间:2017-08-26 12:45:32

标签: php

我需要一个数据" 2.5(0.5)"和" 3.5"
我的模式是'/class="match_total_goal_div">.+</s'
但它没有用。
请帮忙。

<div class="match_total_goal_div">
2.5 (0.5) </div>
<div class="match_half_goal_div hide" ">
</div>
</td>
<td class="text-center corner_goal_range">
<div>
<span class="newlabel">N.A.</span>
</div>
.
.
.
<div class="match_total_goal_div">
3.5 </div>
.
.
.

2 个答案:

答案 0 :(得分:0)

首先,您需要在.+周围添加括号以捕获所需数据。顺便说一下,你需要一个问号:.+?

希望这可以帮到你

$str = '<div class="match_total_goal_div">
2.5 (0.5) </div>
<div class="match_total_goal_div">
3.5 </div>';

$pattern = '/class="match_total_goal_div">(.+?)</s';

preg_match_all($pattern, $str, $matches);

var_dump($matches);

答案 1 :(得分:0)

检查此代码以实现目标

<?php
    $html = '<div class="match_total_goal_div">
    2.5 (0.5) </div>
    <div class="match_half_goal_div hide">
    </div>
    <td class="text-center corner_goal_range"></td>
    <div>
    <span class="newlabel">N.A.</span>
    </div>
    <div class="match_total_goal_div">
    3.5 </div>';
    $DOM = new DOMDocument();
    $DOM->loadHTML($html);
    $finder = new DomXPath($DOM);
    $classname = 'match_total_goal_div';
    $nodes = $finder->query("//*[contains(@class, '$classname')]");
    foreach ($nodes as $node) {
      echo $node->nodeValue."\n";
    }
?>

现场演示:http://sandbox.onlinephpfunctions.com/code/b3e645ac56b9f7bf57d4519abd6b1be90ed87945