Search code examples
phpregexpreg-match

PHP Preg match on html file. Regex


I want to put the French words in a Array.

<?php

$contents = file_get_contents("http://quizlet.com/9117/envol-7-unite-1-presentation-flash-cards/"); 

$pattern = '/<span class="TermText qWord lang-fr">(.*?)</s';

preg_match($pattern,$contents, $matches);

print_r($matches); 

?>

The result of this code is a empty Array.


Solution

  • The source page encloses class values in single quotes. Also you need to use preg_match_all() function to get all results.

    <?php
    
    $contents = file_get_contents("http://quizlet.com/9117/envol-7-unite-1-presentation-flash-cards/"); 
    
    $pattern = "/<span class='TermText qWord lang-fr'>(.*?)\</s";
    
    preg_match_all($pattern,$contents, $matches);
    
    print_r($matches); 
    
    ?>