ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[info] 从字符串中获取城市名称 还可参考rrzuji(yii)代码:common/Tool/AddressTool.php * 代码: ~~~ /** * 从字符串中获取城市名称 * @param $address * @return mixed|string * @author Yang wen hang <1203860880@qq.com> */ public static function getCityName($address) { $city = ''; $address = preg_replace('/(.*?(省|自治区|北京|天津|上海|重庆))/', '', $address, 1); $address = StringOperation::trimStringAllSpace($address); preg_match('/(.*?)(市|自治州|地区|区划|县)/', $address, $matches); if (count($matches) > 0) { $city = $matches[0]; if (!in_array($city, RegionConst::CITY_LIST)) { $city = ''; // 不合法城市 } } return $city; } ~~~ ~~~ /** * 去除字符串中的所有空格 * @author 张晓振 * @date 2021/8/2 * @param string $str * @return string|string[] */ public static function trimStringAllSpace($str) { $search = array(" ", " ", "\n", "\r", "\t"); $replace = array("", "", "", "", ""); return str_replace($search, $replace, $str); } ~~~ * 示例: ~~~ $city = StringOperation::getCityName('广东省深圳市'); echo $city;// 深圳市 ~~~