There is a string

<div class="value1 666"></div> 

How to use PHP and Regex to get an array of two values value1 and 666 ?

My current solution is:

  $regex = "/(?:.*class[ ]*=[ ]*[\"\'])(.*)(?:[\"\'].*)/"; $html = "<div class='value1 666'></div>"; preg_match_all($regex, $html, $results); var_dump($results); 

It has the following disadvantages:

  • in the $results array, the first value is always the input string
  • only works if the attribute is set to one value

    2 answers 2

     preg_match_all("/class\s*=\s*[\"']([^\"'\s]*)\s*([^\"'\s]*)[\"']/", $input, $output); 

    The required data will be contained in $output[1] and $output[2] :

     preg_match_all("/class\s*=\s*[\"']([^\"'\s]*)\s*([^\"'\s]*)[\"']/", "<div class='value1 666'></div><div class='value2 333'></div>", $output); var_dump($output[1]): array( 0 => value1 1 => value2 ) var_dump($output[2]): array( 0 => 666 1 => 333 ) 

    Online expression test

      To parse the html, use a ready-made library, for example phpQuery https://github.com/punkave/phpQuery

       $doc = phpQuery::newDocument('<div class="value1 666"></div>'); $divs = $document->find('div'); $result = array(); foreach ($divs as $_div) { $class = pq($_div)->attr('class'); $result[] = explode(' ', $class); } var_dump($result); 

      regexp is a formal language for searching and manipulating spacing in a text. And not a parser of something ...