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 关注
- 194 浏览
添加回答
举报
