如何动态替代bb标签? [英] How to dynamically replace bb tags?

查看:157
本文介绍了如何动态替代bb标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种智能的方式,允许用户在他们的课程中张贴从语言的语音片段。的语言需要帮助者使用$ this-> Forvo-> word('hola','es')。

I need an intelligent way to allow users to post voice clips from Forvo in their lessons. Forvo requires usage of the helper using $this->Forvo->word('hola', 'es').

一个想法是允许用户使用像[forvo = hola,es]这样的bb代码,但是如何实现呢?我唯一能想出的是使用很多substr,strpos ...这将需要至少35行代码,这将不是很漂亮和安全。

An idea would be to allow the user to use bb-code like [forvo=hola,es], but how to implement this? The only thing I can come up with is using a lot of substr, strpos... which would require at least 35 lines of code and this would not be pretty and secure.

<?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]


推荐答案

谢谢你,Waygood。它适用于preg_replace_callback。

Thank you, Waygood. It works with 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);
?>

这篇关于如何动态替代bb标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆