时间:2021-07-01 10:21:17 帮助过:4人阅读
$str = 'aaa.bbb.ccc.ddd $aaa.bbb.ccc.ddd... aaa.bbb.ccc.ddd'; echo preg_replace_callback('/\$([a-z]+)((?:.[a-z]+)+)/i', 'test', $str); function test($m) { $arr = explode('.',trim($m[0], '$')); $join = array(); foreach($arr as $a) { $join[] = $a[0]; } return '$' . implode($join, '.'); } /**输出: aaa.bbb.ccc.ddd $a.b.c.d... aaa.bbb.ccc.ddd */
$s = 'aaa.bbb.ccc.ddd $aaa.bbb.ccc.ddd... aaa.bbb.ccc.ddd'; echo preg_replace_callback('/\$[^\s]+/', 'back', $s); function back($r) { return preg_replace('/(\w)+/', '$1', $r[0]); }