亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在preg match php代碼上調用函數

如何在preg match php代碼上調用函數

PHP
郎朗坤 2022-12-23 14:02:46
我有代碼..<?phpfunction word() {     $arr = array("/c/","/b/","/c/");     echo $arr[array_rand($arr)];}$text = "a";if(preg_match("$word()", $text)) {     $result = "found";}else{     $result = "not found";}echo $result;?>如何將函數調用word();到preg_match.我想隨機搜索中的單詞preg_match。我試過了,但沒用。如何修復此代碼。
查看完整描述

1 回答

?
慕容708150

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);


查看完整回答
反對 回復 2022-12-23
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號