用PHP寫一個類,實現輸入字符串"abbccdddd",輸出為"1a2b3c4d"
2 回答

交互式愛情
TA貢獻1712條經驗 獲得超3個贊
preg_replace_callback('/(.)(\1*)/', function($match){return mb_strlen($match[0]).$match[1];}, 'abbcccdddd');

智慧大石
TA貢獻1946條經驗 獲得超3個贊
function processStr($str = "abbcccdddd"){ $result = ''; while(strlen($str) > 0){ $tempStr = substr($str,0,1); $result .= substr_count($str, $tempStr).$tempStr; $str = str_replace($tempStr,'',$str); } return $result; }
- 2 回答
- 0 關注
- 554 瀏覽
添加回答
舉報
0/150
提交
取消