简体   繁体   English

解析这样的PHP字符串最简单的方法是什么

[英]what's the easiest way to parse a PHP string like this

I try to use PHP to parse a string to extract info, part of the content looks like this 我尝试使用PHP解析字符串以提取信息,部分内容如下所示

<div>All Versions:</div> 
<div class='rating' role='img' tabindex='-1' aria-label='5 stars, 193984 Ratings'><div>

What's the easiest way in PHP to get these two numbers? PHP获取这两个数字的最简单方法是什么?

(1) the number of stars - which is 5 (1)星数-5

(2) ratings - which is 193984 (2)评分-193984

PS Please don't consider it as HTML parsing but a string PS请不要将其视为HTML解析,而是一个字符串

XML Parser enthusiasts would suggest you use a parser to grab the attribute from the div. XML解析器爱好者会建议您使用解析器从div中获取属性。

$xml = new XMLReader(); //Setup parser
$xml->XML("<div>All Versions:</div><div class='rating' role='img' tabindex='-1' aria-label='5 stars, 193984 Ratings'></div>");
$xml->read();

while($xml->read()) { //Run through each node
    if($xml->getAttribute('class') == 'rating') { //Look for class of 'rating'
        // Break apart aria-label
        list($stars, $ratings) = explode(', ', $xml->getAttribute('aria-label'));
        $stars = intval($stars); //Grab the integer part of the strings
        $ratings = intval($ratings);
        break;
    }
}

$xml->close();

However, this depends on how you would like to identify the div. 但是,这取决于您要如何识别div。 If there are other identifiers that you would like included (maybe more specific ones like an id) you can include them in the if statement. 如果您还想包含其他标识符(例如ID等更具体的标识符),则可以将它们包括在if语句中。

Once you have isolated this part of the page (whether DOM parsing or not), you can extract the two numbers pretty easily with: 一旦隔离了页面的这一部分(无论是否进行DOM分析),就可以使用以下方法轻松提取两个数字:

preg_match('#(\d+) stars, (\d+) Ratings#i', $source, $match);
list(, $stars, $ratings) = $match;

Note that it applies to your example. 请注意,它适用于您的示例。 Should other human-readable attributes be present in other cases, or ordered differently, you would need to eg split up the string on commas, then search each part individually for stars or ratings. 如果在其他情况下还存在其他人类可读属性,或以其他方式排序,则您需要将字符串以逗号分隔,然后分别在每个部分中搜索星号或等级。

$string="<div class='rating' role='img' tabindex='-1' aria-label='5 stars, 193984 Ratings'><div>"
$pattern = '/aria-label=\'(\d+) stars, (\d+) Ratings\'/';
preg_match($pattern, $string, $matches); 
echo "<pre>";
print_r($matches); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在XML中解析混合文本和元素标记的最简单方法是什么? - What's the easiest way to parse mixed text and element markup in XML? 将此字符串(PHP数组)转换为JSON编码的字符串的最简单方法是什么? - What is the easiest way to convert this string (PHP Array) to a JSON encoded string? 用PHP重定向到上一页的最简单方法是什么? - What's the easiest way to redirect to the previous page with PHP? 检查字符串是否为bcrypt哈希的最简单,最快捷的方法是什么? - What's the easiest and quickest way to check if a string is a bcrypt hash? 用 PHP 的 mysqli 做准备好的语句的正确和最简单的方法是什么? - What is the correct and easiest way to do prepared statements with PHP's mysqli? 在PHP中加载一组文字数据的最简单方法是什么? - What's the easiest way to load a set of literal data in PHP? 在Windows XP Professional中测试PHP的最简单方法是什么? - What's the easiest way to test PHP in Windows XP Professional? 通过PHP检索网页内容的最简单方法是什么? - What's the easiest way to retrieve the contents of a webpage via PHP? 在PHP中从HTML提取数据的最简单方法是什么? - What's the easiest way to extract a piece of data from HTML in PHP? 什么是在PHP中解析这样的模板的有效方法? - what is the efficient way to parse a template like this in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM