我需要一种智能的方式,允许用户在他们的课程中发布来自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]发布于 2012-08-16 22:40:38
谢谢你,韦古德它适用于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);
?>https://stackoverflow.com/questions/11988043
复制相似问题