我需要一种智能的方式来允许用户在他们的课程中发布来自Forvo的语音剪辑。Forvo 需要使用 $this->Forvo->word('hola', 'es')来使用帮助程序。
一个想法是允许用户使用像[forvo=hola,es]这样的bb代码,但是如何实现呢?我唯一能想到的就是使用大量的substr,strpos...这将需要至少 35 行代码,这并不漂亮和安全。
<?php
// Replace forvos in the lesson
$lesson = $lesson['Lesson']['body'];
// Todo:
// Replace [forvo=hello,en] by javascript from
// $this->Forvo->word('hello', 'en');
// I seem unable to use regex's like this
$pattern[0] = "/'[forvo'=(.*),(.*)]";
$replace[0] = $this->Forvo->word($1, $2);
echo preg_replace($pattern, $replace, $lesson);
?>
课程示例为:
Pronounciations in Dutch
een [forvo=een,nl]
en [forvo=en,nl]
de [forvo=de,nl]
in [forvo=in,nl]
met [forvo=met,nl]
谢谢你,韦古德。它适用于preg_replace_callback。
<?php
// Replace forvos in the lesson
$lesson = $lesson['Lesson']['body'];
function forvize($match) {
$word = $match[1];
$language = $match[2];
$link = "http://apifree.forvo.com/action/word-pronunciations/format/js-tag /word/".$word."/language/".$language."/order/rate-desc/limit/1/key/123456789/";
$link = file_get_contents($link);
return $link;
}
//URL's
$lesson = preg_replace_callback("/'[forvo'=(.*),(.*)']/", 'forvize', $lesson);
?>