我已經寫了這個腳本,以便能夠在我的代碼中使用bb-code標籤。花了很多時間決定最佳方法並在How to dynamically replace bb tags?詢問後,我決定使用preg_replace_callback。多個項目和preg_replace_callback
有一個問題卻與在同一行
Man [forvo=man,nl]
Mannen [forvo=mannen,nl]
的部分上述工程的多個項目,但下面不會。
Kip [forvo=kip,nl] - Kippen [forvo=kippen,nl]
Bal [forvo=bal,nl] - Ballen [forvo=ballen,nl]
Vrouw [forvo=vrouw,nl] - Vrouwen [forvo=vrouwen,nl]
我知道使用的file_get_contents()是不是一個推薦的選擇,但我也要找到了preg_replace_callback另一種解決辦法,如果我想每行使用多個Forvo中標籤?
<?php
// Replace forvos in the lesson
$lesson_body = $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/API_KEY/";
$link = file_get_contents($link);
return $link;
}
//URL's
$lesson_body = preg_replace_callback("/\[forvo\=(.*),(.*)\]/", 'forvize', $lesson_body);
?>