1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果你讓你的函數(shù)word()返回隨機(jī)字符串而不是回顯它,你可以通過調(diào)用函數(shù)將它用作任何值。
function word() {
$arr = array("/c/","/b/","/c/");
return $arr[array_rand($arr)];
}
if( preg_match(word(), $text) ) {
$result = "found";
}
else {
$result = "not found";
}
echo $result;
如果它更清楚,這與將函數(shù)的結(jié)果存儲(chǔ)在變量中并使用它相同。
這些都是一樣的:
// Writing the pattern in place.
preg_match("/a/", $text);
// Storing it in a variable before use.
$to_match = "/a/";
preg_match($to_match, $text);
// Storing it in a variable where the value is returned from a function.
$to_match = word();
preg_match($to_match, $text);
// Using a function directly in the call to `preg_match`.
preg_match(word(), $text);
- 1 回答
- 0 關(guān)注
- 106 瀏覽
添加回答
舉報(bào)