好的,所以说我有一个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>
Run Code Online (Sandbox Code Playgroud)
然后是一种使用PHP动态吐出它们(span标签中的单词)作为模板文件中的有序列表的方法吗?
像这样:
<h3>What John Did Today</h3>
<ol>
<li>bakery</li>
<li>park</li>
<li>swings</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
如果有人能指出如何做这样的事情的正确方向,那将非常感激.谢谢.
$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";
Run Code Online (Sandbox Code Playgroud)