ကဗ်ာ/ရသ/နည္းပညာ

php functions

Sep 16, 2013
Use the strtoupper() or strtolower() functions:
<?php
// define string
$rhyme = "And all the king's men couldn't put him together again";
// uppercase entire string
// result: "AND ALL THE KING'S MEN COULDN'T PUT HIM TOGETHER AGAIN"
$ucstr = strtoupper($rhyme);
echo $ucstr;
// lowercase entire string
// result: "and all the king's men couldn't put him together again"
$lcstr = strtolower($rhyme);
echo $lcstr;
?>
strtoupper() method ကုိသံဳးရင္ ေရးသမွ်ကုိ စာလံုးၾကီးနဲ႕ Result ျပေပးႏိုင္ပါတယ္။
နမူနာကုိၾကည့္ရင္နားလည္ပါလိမ့္မယ္။
isset() and trim() functions: က empty string value ျဖစ္ေနသလားစစ္ေဆးႏိုင္ပါတယ္။
<?php
// define string
$str = "  ";
// check if string is empty
// result: "Empty"
echo (!isset($str) || trim($str) == "") ? "Empty" : "Not empty";
?>
ေနာက္ထပ္သံဳးလုိ႕ရတာက empty() function ျဖစ္ပါတယ္။ သူက String 0 ျဖစ္ရင္ လည္း Empty
လို႕သတ္မွတ္တာေၾကာင့္ နည္းနည္းေတာ့လြဲေခ်ာ္ႏိုင္ေျခရွိေနပါတယ္။Php မွာ Boolean Data ရဲ့ Default
တန္ဖိုး ဟာ False(0) ျဖစ္ပါတယ္။
<?php
// define string
$str = "0";
// check if string is empty
// result: "Empty"
echo (empty($str)) ? "Empty" : "Not empty";
?>
str_repeat($ဗယ္ရီေရဘယ္, အေရအတြက္)  ဒီ Method ကေတာ့ စာတစ္ေၾကာင္းကုိ အၾကိမ္ၾကိမ္ ျပန္ထုတ္ခိုင္းလို႕ရေစပါတယ္ ။ေအာက္က ဥပမာေလးကိုၾကည့္လုိက္ပါ။
<?php
// define string
$laugh = "ha ";
// repeat string    
// result: "ha ha ha ha ha ha ha ha ha ha "
$rlaugh = str_repeat($laugh, 10);
echo $rlaugh;
?>
ေနာက္ထပ္မွတ္ထားရမယ့္ Method ကေတာ့ substr() ျဖစ္ပါတယ္။သူကစာလံုးေရသတ္မွတ္ၿပီးျဖတ္ထုတ္ေပး
ႏိုင္ပါတယ္။ေအာက္ကဥပမာမွာ Function တစ္ခုဖန္တီးၿပီး အသံုးျပဳသြားတာကုိေတြ႕ႏိုင္ပါတယ္။
<?php
function truncateString($str, $maxChars=40, $holder="...") {
    // check string length
    // truncate if necessary
    if (strlen($str) > $maxChars) {
        return trim(substr($str, 0, $maxChars)) . $holder;
    } else  {
        return $str;
    }
}
// define long string
$str = "Just as there are different flavors of client-side scripting,
there are different languages that can be used on
the server as well.";
// truncate and print string
// result: "Just as there are different flavours of..."
echo truncateString($str);
// truncate and print string
// result: "Just as there are di >>>"
echo truncateString($str, 20, " >>>");
?>

No comments:

Post a Comment