在php中使用preg_match获取第二项

I am getting text between two tags with PHP (from a HTML).

a sample code i use is this :

function GDes($url) {
    $fp = file_get_contents($url);
    if (!$fp) return false;

    $res = preg_match("/<description>(.*)<\/description>/siU", $fp, $title_matches);

    if (!$res)  return false; 

    $description = preg_replace('/\s+/', ' ', $title_matches[1]);
    $description = trim($description);
    return $description;
}

It gives between the description tags, But my problem is that if the page have to description tags, it will give the first one that i don't need it.

I need to get the second one.

For example, If my HTML is this :

<description>No need to this</description>
<description>I NEED THIS ONE</description>

I need to give the second description tag with that function above.

What changes the function needed ?

Use preg_match_all instead. It will create an array with all matches. You can keep your code as is, just replace preg_match with preg_match_all.

Then you have to use $title_matches[1][1] instead of $title_matches[1] in your preg_replace call, since the $title_matches is now a multidimensional array.