gpt4 book ai didi

php - 在 PHP 中提取 标签内容

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:02 25 4
gpt4 key购买 nike

假设我有一个 WordPress 帖子,并且某些单词包含在 span 标记中。

例如:

<p>John went to the <span>bakery</span> today,
and after picking up his favourite muffin
he made his way across to the <span>park</span>
and spent a couple hours on the <span>swings</span>
with his friends.</p>

那么是一种使用 PHP 将它们(span 标签中的单词)动态吐出为我的模板文件中的有序列表的方法吗?

像这样:

<h3>What John Did Today</h3>
<ol>
<li>bakery</li>
<li>park</li>
<li>swings</li>
</ol>

如果有人能指出如何做这样的事情的正确方向,将不胜感激。谢谢。

最佳答案

$str = '<p>John went to the <span>bakery</span> today, and after picking up his favourite muffin     he made his way across to the <span>park</span> and spent a couple hours on the <span>swings</span> with his friends.</p>';

$d = new DomDocument;
$d->loadHTML($str);

$xpath = new DOMXPath($d);
echo "<h3>What John Did Today</h3>\n";
echo "<ol>\n";
foreach ($xpath->query('//span') as $span)
echo "<li>".$span->nodeValue."</li>\n";
echo "</ol>\n";

关于php - 在 PHP 中提取 <span> 标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6448243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com