使用PHP从HTML字符串获取特定数据的快速方法 [英] Fast way to get specific data from HTML string using PHP
问题描述
我避免了很多来这里分享我的问题.我在Google上搜索了很多,找到了一些解决方案,但未得到证实. 首先,我解释我的问题.
I avoided a lot to come here share my problem. I have googled a lot and find some solutions but not confirmed. First I explain My Problem.
我的网站上有一个CKEditor,可让用户发表评论.假设用户单击两个帖子以多引用它们,则数据将在CKEditor中是这样的
I have a CKEditor in my site to let the users post comments. Suppose a user clicks two posts to Multi quote them, the data will be like this in CKEditor
<div class="quote" user_name="david_sa" post_id="223423">
This is Quoted Text
</div>
<div class="quote" user_name="richard12" post_id="254555">
This is Quoted Text
</div>
<div class="original">
This is the Comment Text
</div>
我想分别在php中获取所有元素,如下所示
I want to get all the elements separately in php as below
user_name = david_sa
post_id = 223423;
quote_text = This is Quoted Text
user_name = david_sa
post_id = richard12;
quote_text = This is Quoted Text
original_comment = This is the Comment Text
我想在PHP中以上述格式获取数据.我已经在Google上搜索,发现我的问题附近有preg_match_all()PHP函数,该函数使用REGEX来匹配字符串模式.但是我不确定这是否是合法有效的解决方案,还是有更好的解决方案.如果您有更好的解决方案,请建议我.
I want to get the data in above format in PHP. I have googled and found the preg_match_all() PHP function near to my problem, that uses the REGEX to match the string patterns. But I am not confirmed that is it a legitimate and efficient solution or there is some better solution. If You have any better solution Please Suggest Me.
推荐答案
您可以使用 DOMDocument
和 DOMXPath
.只需很少的代码行即可解析HTML并从中提取几乎所有内容.
You can use DOMDocument
and DOMXPath
for this. It takes very few lines of code to parse HTML and extract just about anything from it.
$doc = new DOMDocument();
$doc->loadHTML(
'<html><body>' . '
<div class="quote" user_name="david_sa" post_id="223423">
This is Quoted Text
</div>
<div class="quote" user_name="richard12" post_id="254555">
This is Quoted Text
</div>
<div class="original">
This is the Comment Text
</div>
' . '</body></html>');
$xpath = new DOMXPath($doc);
$quote = $xpath->query("//div[@class='quote']");
echo $quote->length; // 2
echo $quote->item(0)->getAttribute('user_name'); // david_sa
echo $quote->item(1)->getAttribute('post_id'); // 254555
// foreach($quote as $div) works as expected
$original = $xpath->query("//div[@class='original']");
echo $original->length; // 1
echo $original->item(0)->nodeValue; // This is the Comment Text
如果您不熟悉 XPath语法,则下面是一些示例,帮助您入门.
If you are not familiar with XPath syntax then here are a few examples to get you started.
这篇关于使用PHP从HTML字符串获取特定数据的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!