如何使用 PHP 解析内容以将假列表替换为真实列表?
可能的重复:
使用 PHP 解析 HTML 的最佳方法
所以我有大量的条目在我的数据库中输入列表,但它们不是真正的列表,我需要将它们转换为实际列表。
这就是我所拥有的:
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.
需要更改为:
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.
它不必像那样格式化,可以全部粉碎在一起。我不在乎。
谢谢。
忘了提及 will be 列表的两侧都有 HTML 数据。
我还有 SimpleDOM 解析器。不太有兴趣再买一个,但如果有一个非常容易使用的可以解决这个问题,那将会很有帮助。
再次感谢。
Possible Duplicate:
Best methods to parse HTML with PHP
So I have a ton of entries in my database where lists where entered, but they're not real lists and i need to convert them to actual lists.
Here's what I have:
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.
Needs to change to:
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.
It doesn't have to be formatted like that, could just be all smashed together. I don't care.
Thanks.
Forgot to mention there is HTML data on both sides of the would be list.
Also I've got the SimpleDOM parser. Not really interested in getting another one, but if there's a really easy one to use that would take care of this it would be helpful.
Thanks, again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会因为不使用 DOM 解析器而受到谴责,但事情就是这样。这只是一个简单的字符串操作,不需要正则表达式。
您只需将
打开/关闭标记替换为
< ;/ul>
.更新修复了问题、之前的内容和内容的更新。列表之后...:
I'm going to get reprimands for not using a DOM parser, but here goes. This is just a simple string operation, no regex needed.
You just need to replace the
<p>
open/close tags with<li>
open/close tags, and wrap it in<ul></ul>
.Updated Fixed to account for updates to question, stuff before & after the list...:
你可以只使用
Str_replace
将所有
替换为
的
You could just use
Str_replace
where you replace all the
<p>
with<li>
and all the
</p>
with</li>
更新:
我之前遇到过这个问题,其中有一堆带有“假”列表的数据,使用缩进和不同的字符作为项目符号,所以我只是做了这个小函数。
UPDATE:
I've run in to this problem before where there's a bunch of data with 'fake' lists using indenting and different chars as the bullet so I just made this little function.