Re: [請益] 請問如何判斷大小寫?

看板PHP作者 (選擇那刻 才算開始)時間15年前 (2008/11/13 03:56), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
※ 引述《aquarianboy (我是沒牌的營養師!)》之銘言: : 我剛試著用strtoupper玩看看,不過感覺應該比較沒效率 :) : <?php : $test = "This is a Test"; : for($i = 0; $i< strlen($test); $i++) : { : $c = $test[$i]; : $test[$i] = ($c === strtoupper($c)) ? strtolower($c) : strtoupper($c); : //或改用上面提到的ctype_upper,看起來更乾淨一些... : //$test[$i] = (ctype_upper($c)) ? strtolower($c) : strtoupper($c); : } : echo $test; // tHIS IS A tEST 似乎可以這樣做 不過不知道有沒有比較好XD <?php function reverse_case($matches) { return ctype_upper($matches[0]) ? strtolower($matches[0]) : strtoupper($matches[0]); } $subject = 'This is a Test'; echo preg_replace_callback('/[A-Z]+|[a-z]+/', 'reverse_case', $subject); ?> preg_replace_callback(pattern, callback, subject); 裡面呼叫的函數用法 似乎是傳入該次match到的東西 要回傳目標字串 想法: 找到連續大寫或連續小寫 判斷大小寫後作轉換 -- 這個問題好像還蠻有趣的XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.203.6
文章代碼(AID): #196pJvzp (PHP)
文章代碼(AID): #196pJvzp (PHP)