This question already has an answer here:
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.
</div>
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);
?>
If you want to get all inner texts of <span>
tags having lang-fr
in their class
attribute value, you can use the following DOMDocument/DOMXPath based solution:
$contents = file_get_contents("http://quizlet.com/9117/envol-7-unite-1-presentation-flash-cards/");
$dom = new DOMDocument;
@$dom->loadHTML($contents, LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$spans = $xp->query('//span[contains(@class,"lang-fr")]');
$arr = array();
foreach ($spans as $span) {
array_push($arr, $span->nodeValue);
}
print_r($arr);
See IDEONE demo
The xpath is '//span[contains(@class,"lang-fr")]'
here. You can make it stricter to only get all span tags with class attribute value equal to "TermText qWord lang-fr": '//span[@class="lang-fr"]'
.
This solution relieves you from the problem of matching this or that type of delimiting attribute values in HTML. And many other issues related to regex HTML parsing.