如何使用PHP解析内容以用真实列表替换假列表?
可能重复:如何使用PHP解析内容以用真实列表替换假列表?,php,preg-replace,preg-match,Php,Preg Replace,Preg Match,可能重复: 因此,我的数据库中有大量的条目,其中包含输入的列表,但它们不是真实的列表,我需要将它们转换为实际的列表 以下是我所拥有的: Other HTML data here. <p>ñ Line of data</p> <p>ñ Another line of data</p> <p>ñ Yet another line of data</p> <
因此,我的数据库中有大量的条目,其中包含输入的列表,但它们不是真实的列表,我需要将它们转换为实际的列表 以下是我所拥有的:
Other HTML data here.
<p>ñ Line of data</p>
<p>ñ Another line of data</p>
<p>ñ Yet another line of data</p>
<p>ñ Still more data</p>
More HTML data here.
此处显示其他HTML数据。
&恩蒂尔德;数据行
&恩蒂尔德;另一行数据
&恩蒂尔德;还有一行数据
&恩蒂尔德;更多数据
这里有更多的HTML数据。
需要更改为:
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数据。
- 数据行
- 另一行数据
- 还有一行数据
- 更多数据
这里有更多的HTML数据。
它不需要像那样格式化,只需要把所有的东西砸在一起就可以了。我不在乎
谢谢
忘了提一下在列表的两边都有HTML数据 我还有SimpleDOM解析器。我不太想再买一个,但是如果有一个很容易使用的,可以解决这个问题的话,它会很有帮助的 再次感谢。您可以使用 将所有
替换为
所有的
和
我都会因为没有使用DOM解析器而受到谴责,但现在开始。这只是一个简单的字符串操作,不需要正则表达式
您只需将
打开/关闭标记替换为
打开/关闭标记,并将其包装在
中
更新修复了列表前后问题、内容更新的帐户…:
$original = "Stuff here
<p>ñ Line of data</p>
<p>ñ Another line of data</p>
<p>ñ Yet another line of data</p>
<p>ñ 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>ñ ", "<li>", $listpart);
$fixed = str_replace("</p>", "</li>", $fixed);
// Stick it all back together
$fixed = "$stuffbefore\n<ul>$fixed</ul>\n$stuffafter";
$original=“这里的东西
&数据行
ñ;另一行数据
ñ;另一行数据
&更多数据
“其他材料”;
//在列表之前和之后存储内容
$stuffbefore=substr($original,0,stripos($original,));
$stuffafter=substr($original,stripos($original,“”)+strlen(“”);
//把清单上的东西删掉
$listpart=substr($original,strlen($stuffbefore));
//把清单上的东西删掉
$listpart=substr($listpart,0,strlen($listpart)-strlen($stuffafter));
$fixed=str_replace(“ñ;”,“”,$listpart);
$fixed=stru替换(“”、“ ”、$fixed);
//把它粘在一起
$fixed=“$stuffbefore\n$fixed
\n$stuffbefore”;
更新:
我以前遇到过这个问题,其中有一堆数据带有“假”列表,使用缩进和不同的字符作为项目符号,所以我只做了这个小函数
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>ñ FIRST LIST ITEM</p>
<p>ñ MIDDLE LIST ITEM</p>
<p>ñ LAST LIST ITEM</p>
<p>STUFF AFTER</p>";
echo make_real_list("/\<p\>ñ (.*?)\<\/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>
函数make_real_list($regex,$content,$type=“无序”){
preg_match_all($regex,$content,$matches);
$matches=$matches[0];
$count=sizeof($matches);
如果($type==“无序”):
$outer_start=“- ”;
$outer_end=“
- 第一个列表项
- 中间列表项
- 最后一项
+1在这样一个简单的任务中没有使用DOM解析器,我向您致意。我稍微修改了我需要的内容,因为列表两边都有需要解析的数据。所以这个解决方案不适用于这种情况,抱歉。@Tomas这使它变得更复杂了,但是请参见上面的必要更改。是的,这也不行,因为在文件前后都有html标记,还有很多标记。所以我不确定该往哪个方向走。如果您想使用
preg\u replace
:echo“\n”。preg_replace(“~ñ;*(.*?~”,“\t- $1
\n”,$content)。“
\n”代码>