-2

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.

  • 1) http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags 2) That string doesn't seem to appear on that URL provided. (The string in the source uses single quote encapsulation for the class definition, for reference). – Jonnix Sep 03 '15 at 12:39
  • @JonStirling, for your #2 comment, the string does exist on line 895 – CodeGodie Sep 03 '15 at 12:42
  • @CodeGodie No it doesn't. Look more closely. – Jonnix Sep 03 '15 at 12:43
  • Ahh.. it does exist, except it has single quotes `'` rather than double quotes `"` – CodeGodie Sep 03 '15 at 12:45

2 Answers2

0

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-2

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); 

?>
Elias
  • 95
  • 4