简体   繁体   中英

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

<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?

(1) the number of stars - which is 5

(2) ratings - which is 193984

PS Please don't consider it as HTML parsing but a string

XML Parser enthusiasts would suggest you use a parser to grab the attribute from the 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. 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.

Once you have isolated this part of the page (whether DOM parsing or not), you can extract the two numbers pretty easily with:

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM