使用 simple_html_dom 解析 ul

发布于 2024-11-04 21:17:07 字数 1776 浏览 1 评论 0原文

我想获取这个 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>

我现在有这个,但我想这样做而不是为每个跨度编写相同的代码。

//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;

I would like to get the inner text of each span in this 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>

I have this right now but I want to do it without writing the same code over for each span.

//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;

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

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

发布评论

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

评论(2

朦胧时间 2024-11-11 21:17:07

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

$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";
}

you're already using a parser, why would you want to use a regex? You can access each span's inner text quite easily:

$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";
}
预谋 2024-11-11 21:17:07

查看 preg_match_all。它将给出所有匹配的数组

Have a look at preg_match_all. It will give an array of all matches

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