如何使用PHP解析内容以用真实列表替换假列表


How do I parse content to replace fake lists with real lists using PHP?

可能重复:
用PHP 解析HTML的最佳方法

所以我在数据库中有很多条目,其中输入了列表,但它们不是真正的列表,我需要将它们转换为实际的列表。

这是我的:

Other HTML data here.
<p>&ntilde; Line of data</p>
<p>&ntilde; Another line of data</p>
<p>&ntilde; Yet another line of data</p>
<p>&ntilde; Still more data</p>
More HTML data here.

需要更改为:

Other HTML data here.
<ul>
    <li>Line of data</li>
    <li>Another line of data</li>
    <li>Yet another line of data</li>
    <li>Still more data</li>
</ul>
More HTML data here.

它不必像那样格式化,可以把所有东西都砸在一起。我不在乎。

谢谢。


忘了提一下,列表两边都有HTML数据。

我还有SimpleDOM解析器。对再买一个不太感兴趣,但如果有一个非常简单的可以解决这个问题的,那会很有帮助。

再次感谢。

我会因为没有使用DOM解析器而受到谴责,但这里是。这只是一个简单的字符串操作,不需要正则表达式。

您只需要将<p>打开/关闭标记替换为<li>打开/关闭标签,并将其包装在<ul></ul>中。

已更新已修复对问题的更新,在&在列表之后…:

$original = "Stuff here
<p>&ntilde; Line of data</p>
<p>&ntilde; Another line of data</p>
<p>&ntilde; Yet another line of data</p>
<p>&ntilde; Still more data</p>
Other stuff";
// Store stuff before & after the list
$stuffbefore = substr($original, 0, stripos($original, "<p>"));
$stuffafter = substr($original, strripos($original, "</p>") + strlen("</p>"));
// Cut off the stuff before the list
$listpart = substr($original, strlen($stuffbefore));
// Cut off stuff after the list
$listpart = substr($listpart, 0, strlen($listpart) - strlen($stuffafter));
$fixed = str_replace("<p>&ntilde; ", "<li>", $listpart);
$fixed = str_replace("</p>", "</li>", $fixed);
// Stick it all back together
$fixed = "$stuffbefore'n<ul>$fixed</ul>'n$stuffafter";

您可以使用Str_replace将所有<p>替换为<li>而CCD_ 6和CCD_

更新:我以前遇到过这个问题,那里有一堆带有"伪"列表的数据,使用缩进和不同的字符作为项目符号,所以我只做了这个小函数。

function make_real_list($regex, $content, $type="unordered"){
    preg_match_all($regex, $content, $matches);
    $matches    = $matches[0];
    $count  = sizeof($matches);
    if($type=="unordered"):
        $outer_start    = "<ul>";
        $outer_end      = "</ul>";
    else:
        $outer_start    = "<ol>";
        $outer_end      = "</ol>";
    endif;
    $i = 1;
    foreach($matches as $match):
        if($i==1):
            $replace    = preg_replace($regex, '<li>$1</li>', $match, 1);
            $match  = preg_quote($match, "/");
            $content    = preg_replace("/$match/", ($outer_start?$outer_start:'').$replace, $content);
        elseif($i==$count):
            $replace    = preg_replace($regex, '<li>$1</li>', $match, 1);
            $match  = preg_quote($match, "/");
            $content    = preg_replace("/$match/", $replace.($outer_end?$outer_end:''), $content);
        else:
            $content    = preg_replace($regex, '<li>$1</li>', $content, 1);
        endif;
        $i++;
    endforeach;
    return $content;
}
$content = "<p>STUFF BEFORE</p>
<p>&ntilde; FIRST LIST ITEM</p>
<p>&ntilde; MIDDLE LIST ITEM</p>
<p>&ntilde; LAST LIST ITEM</p>
<p>STUFF AFTER</p>";
echo make_real_list("/'<p'>&ntilde; (.*?)'<'/p'>/", $content);
//OUTPUT
<p>STUFF BEFORE</p> 
<ul>
    <li>FIRST LIST ITEM</li> 
    <li>MIDDLE LIST ITEM</li> 
    <li>LAST LIST ITEM</li>
</ul> 
<p>STUFF AFTER</p>