私は最初のselect...fromを見つけてそれだけを置き換えたいのですが、次のコードはsqlクエリのすべてのselect..fromを置き換えます。最初のselect..fromが必要です。
preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql);
私は最初のselect...fromを見つけてそれだけを置き換えたいのですが、次のコードはsqlクエリのすべてのselect..fromを置き換えます。最初のselect..fromが必要です。
preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql);
preg_replace()には'limit' と呼ばれる 4 番目のオプション パラメータがあり、これは置換の数を定義し、デフォルトは -1 (無制限を意味する)です。
preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql, 1);