1
下面的例程對輸入的超文本流進行兩次掃描。第一遍是用戶定義的短語選項的自旋替換。第二遍是在下面的doReplace函數中的標籤集合上進行查找替換。這個例程如何更好地優化?
我只是在尋找如何優化它的建議。我沒有任何性能問題。但我想要構建可擴展性。
/* FIND REPLACE SPIN
--------------------------------------------------------------------*/
function doReplace($content)
{
// content is a precompiled text document formatted with html and
// special using replacement tags matching the $tags array collection below
$tags = array('[blog-name]', '[blog-url]', '[blog-email]');
$replacements = array('value1', 'value2', 'value3');
$content = str_replace($tags, $replacements, $content);
return $content;
}
function doSpin($content) {
// the content also has phrase option tags denoted by [%phrase1|phrase2_|phrase3%]
// delimiters throughout the text.
return preg_replace_callback('!\[%(.*?)%\]!', 'pick_one', $content);
}
function pick_one($matches) {
$choices = explode('|', $matches[1]);
return $choices[rand(0, count($choices)-1)];
}
$my_source_page = file_get_contents('path/to/source';}
$my_source1_spin = doSpin($my_source_page);
$my_source1_replace = doReplace($my_source1_spin);
$my_source1_final = addslashes($my_source1_replace);
//Now do something with $my_source1_final
雖然這應該不會影響性能,您應該使用array_rand()在PICK_ONE()函數: 回報$選擇[array_rand( $選擇)]。 – Inspire 2010-03-07 15:41:06