Re: [請益] 請問如何判斷大小寫?
※ 引述《Huangs (我是大笨蛋)》之銘言:
: ※ 引述《c2yuan (西西‧樣)》之銘言:
: : 各位高手們好.
: : 想要來寫個PHP, 把一段文章中某些特定的字母從小寫換成大寫, 若是大寫則換成小寫,
: : 可是PHP的函式裡好像都只有純粹換成小寫, 或換成大寫,
: : (像是﹕lcfirst(),strtolower(),strtoupper(),ucwords())
: : 而無法逹到"原本小寫的就變大寫, 原本大寫的變小寫"...
: : 請問有什麼好方法呢?
: : 謝謝囉...
: 用 ord() 要自己 handle ASCII 裡大小寫字母的編號範圍,比較不漂亮。
: 關於大小寫的判斷,
: 用 php 內建的 Character type 系列 function 來處理比較好:
: if (ctype_upper($char))
: echo strtolower($char);
: else if (ctype_lower($char))
: echo strtoupper($char);
: else
: .....
我剛試著用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
--
誠徵PHP網站工程師,熟MVC架構佳 http://blog.eddie.com.tw/
全職、兼職可,意者請敲 :) http://www.eddie.com.tw/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.139.9
討論串 (同標題文章)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章