1 回答
TA貢獻1831條經驗 獲得超4個贊
如果你讓你的函數word()返回隨機字符串而不是回顯它,你可以通過調用函數將它用作任何值。
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;
如果它更清楚,這與將函數的結果存儲在變量中并使用它相同。
這些都是一樣的:
// 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 關注
- 130 瀏覽
添加回答
舉報
