/ / PHPを使用してリストをループし、異なる形式に設定する-php

PHPを使用してリストとフォーマットをループする - PHP

PHPは、HTML形式のリストであるテーブルからデータを取得します。

<ul>
<li>first line of text</li>
<li>second line of text</li>
<li>third line of text</li>
<li>forth line of text</li>
<li>fifth line of text</li>
<li>sixth line of text</li>
<li>seventh line of text</li>
</ul>

出力されるもの:

<?php echo $lines; ?>

ただし、可能であればそのリストを取得して次の形式で出力できるように、foreachを使用したいと思います

<span>first line of text</span>
<span>second line of text</span>
<span>third line of text</span>
<span>forth line of text</span>
<span>fifth line of text</span>
<span>sixth line of text</span>
<span>seventh line of text</span>

ulとliを取り除き、liをspanタグで置き換えたいのですが、これは可能ですか?

回答:

回答№1の場合は3

すばやく汚れた:

$lines = str_replace("<ul>", "", $lines);
$lines = str_replace("</ul>", "", $lines);
$lines = str_replace("<li>", "<span>", $lines);
$lines = str_replace("</li>", "</span>", $lines);

以下:

$array = array("<ul>", "</ul>", "<li>", "</li>");
$replace = array("", "", "<span>", "</span>");
$lines = str_replace($array, $replace, $lines);