Commit c93dd573 authored by arnaudbey's avatar arnaudbey
Browse files

handle english

parent ace19e03
......@@ -4,13 +4,29 @@ namespace Innova\WiktionaryBundle\Manager;
class WiktionaryManager
{
protected $patterns = [
'fr' => [
'definitions' => '/<span[\s\S]+?id="fr"[\s\S]*?(<ol>[\s\S]*?<\/ol>)/',
'example' => '/<ul>.*?<\/ul>/s',
],
'en' => [
'definitions' => '/<p><strong class="Latn headword" lang="en.*?<ol>(.*?)<\/ol>/s',
'example' => '/<(d|u)l>.*?<\/(d|u)l>/s',
],
];
public function getDefinitions($form, $language)
{
$wiktionaryOutput = $this->requestWiktionary($form, $language);
if ($wiktionaryOutput) {
if (preg_match('/<span[\s\S]+?id="fr"[\s\S]*?(<ol>[\s\S]*?<\/ol>)/', $wiktionaryOutput, $matches)) {
return $matches[1];
if (preg_match_all($this->patterns[$language]['definitions'], $wiktionaryOutput, $matches)) {
$output = '';
foreach ($matches[1] as $defgroup) {
$output .= $defgroup;
}
return $output;
}
}
......@@ -22,7 +38,7 @@ class WiktionaryManager
$definitions = $this->getDefinitions($form, $language);
if ($definitions) {
$definitions = $this->removeExamples($definitions);
$definitions = $this->removeExamples($definitions, $language);
if (preg_match_all('/<li>(.*?)<\/li>/s', $definitions, $matches)) {
$definitions = $matches[1];
$key = array_rand($definitions);
......@@ -35,9 +51,9 @@ class WiktionaryManager
return;
}
private function removeExamples($definitions)
private function removeExamples($definitions, $language)
{
$definitions = preg_replace("/<ul>.*?<\/ul>/s", '', $definitions);
$definitions = preg_replace($this->patterns[$language]['example'], '', $definitions);
return $definitions;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment