首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何动态替换bb标签?

如何动态替换bb标签?
EN

Stack Overflow用户
提问于 2012-08-16 21:18:21
回答 1查看 111关注 0票数 1

我需要一种智能的方式,允许用户在他们的课程中发布来自Forvo的语音剪辑。Forvo需要使用$this->Forvo->word('hola','es')来使用帮助器。

一个想法是允许用户使用像forvo=hola,es这样的bb代码,但是如何实现呢?我唯一能想到的就是使用大量的substr,strpos...这将需要至少35行代码,这将是不美观和安全的。

代码语言:javascript
复制
<?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);

?>

课程的示例如下:

代码语言:javascript
复制
Pronounciations in Dutch
    een [forvo=een,nl]
    en [forvo=en,nl]
    de [forvo=de,nl]
    in [forvo=in,nl]
    met [forvo=met,nl]
EN

回答 1

Stack Overflow用户

发布于 2012-08-16 22:40:38

谢谢你,韦古德它适用于preg_replace_callback。

代码语言:javascript
复制
<?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);
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11988043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档