PHP 之查找字符串位置函数封装
/** * 正数查找字符串n次出现的位置 * @param $str * @param $find * @param $n * @return bool|int */ function str_n_pos($str, $find, $n = 1) { $pos_val = 0; for ($i = 1; $i <= $n; $i++) { $pos = strpos($str, $find); $str = substr($str, $pos + 1); $pos_val = $pos + $pos_val + 1; } return $pos_val - 1; } /** * 逆数查找字符串n次出现的位置 * @param $str * @param $find * @param $n * @return bool|int */ function str_n_rpos($str, $find, $n = 1) { $pos_val = 0; for ($i = 1; $i <= $n; $i++) { $pos = strrpos($str, $find); $str = substr($str, 0, $pos); $pos_val = $pos; } return $pos_val; }