1
我需要一种智能的方式让用户在课程中发布来自Forvo的语音片段。 Forvo需要使用$ this-> Forvo-> word('hola','es')来使用帮助器。如何动态替换bb标签?
一个想法是允许用户使用像[forvo = hola,es]这样的bb-code,但是如何实现呢?我唯一能想到的就是使用很多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()http://php.net/manual/en/function.preg-replace-callback.php – Waygood 2012-08-16 13:56:25