jannik
10/12/2017 - 8:04 AM

Get nth paragraph of html element

Get nth paragraph of html element

function getParagraph($number, $string){
    $index = $number-1;
    $matches = [];

    if(!preg_match_all('#<p>(.*?)<\/p>#', $string, $matches))
        throw new Exception('Geen beschrijving gecvonden');

    $matches = $matches[1];
    if(!array_key_exists($index, $matches))
        throw new Exception("Only ".count($matches)." were found, you requested number {$number}");

    return $matches[$index];
}