Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用simple_html_dom解析ul_Php_Regex_Simple Html Dom - Fatal编程技术网

Php 使用simple_html_dom解析ul

Php 使用simple_html_dom解析ul,php,regex,simple-html-dom,Php,Regex,Simple Html Dom,我想得到这个ul中每个跨度的内部文本 <ul class="alternatingList"> <li><strong>Last Played</strong><span id="ctl00_mainContent_lastPlayedLabel">04.29.2011</span></li> <li class="alt"><strong>Armory Complet

我想得到这个ul中每个跨度的内部文本

<ul class="alternatingList">
     <li><strong>Last Played</strong><span id="ctl00_mainContent_lastPlayedLabel">04.29.2011</span></li>
     <li class="alt"><strong>Armory Completion</strong><span id="ctl00_mainContent_armorCompletionLabel">52%</span></li>
     <li><strong>Daily Challenges</strong><span id="ctl00_mainContent_dailyChallengesLabel">127</span></li>
     <li class="alt"><strong>Weekly Challenges</strong><span id="ctl00_mainContent_weeklyChallengesLabel">4</span></li>
     <li><strong>Matchmaking MP Kills</strong><span id="ctl00_mainContent_matchmakingKillsLabel">11,280 (1.18)</span></li>
     <li class="alt"><strong>Matchmaking MP Medals</strong><span id="ctl00_mainContent_medalsLabel">15,383</span></li>
     <li><strong>Covenant Killed</strong><span id="ctl00_mainContent_covenantKilledLabel">10,395</span></li>
     <li class="alt"><strong>Player Since</strong><span id="ctl00_mainContent_playerSinceLabel">09.13.2010</span></li>
     <li class="gamesPlayed"><strong>Games Played</strong><span id="ctl00_mainContent_gamesPlayedLabel">975</span></li>
</ul>
  • 最后一场比赛2011年4月29日
  • 武器库完工率52%
  • 每日挑战127
  • 每周挑战4
  • 配对MP杀死11280(1.18)
  • 配对MP奖牌
  • 圣约被杀10395
  • 自2010年9月13日以来的球员
  • 玩过的游戏975
我现在有这个,但我不想为每个跨度编写相同的代码

//pull last played text
$last_played = '';
$last_played_el = $html->find(".alternatingList");
if (preg_match('|<span[^>]+>(.*)</span>|U', $last_played_el[0], $matches)) {
    $last_played = $matches[1];
}

echo $last_played;
//提取上次播放的文本
$last_played='';
$last_playel=$html->find(“.alternatingList”);
if(preg|U match('|]+>(.*)U',$last|U played_el[0],$matches)){
$last_played=$matches[1];
}
echo$last_播放;

看一看。它将提供一个包含所有匹配项的数组

如果您已经在使用解析器,为什么要使用正则表达式?您可以非常轻松地访问每个跨度的内部文本:

$html = new simple_html_dom();  

// Load from a string, where $raw is your UL or the page its on  
$html->load($raw); 

foreach($html->find('.alternatingList span')as $found) {
    echo $found->innertext . "\n";
}

实际上,“多行”与
preg\u match\u all
无关。您仍然需要设置DOT_ALL修饰符(
s
我想)以匹配EOL。