PHP sprintf() လုပ်ဆောင်ချက်
ဥပမာ
အငြင်းအခုံအဖြစ် သတ်မှတ်ပေးထားသော ကိန်းရှင်တစ်ခုဖြင့် ရာခိုင်နှုန်း (%) ကို အစားထိုးပါ။
<?php
$number = 9;
$str = "Beijing";
$txt = sprintf("There are %u million bicycles in %s.",$number,$str);
echo $txt;
?>
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
sprintf() လုပ်ဆောင်ချက်သည် ကိန်းရှင်တစ်ခုသို့ ဖော်မတ်ချထားသည့် စာကြောင်းတစ်ခုကို ရေးပေးသည်။
arg1၊ arg2၊ ++ ဘောင်များကို ပင်မစာကြောင်းတွင် ရာခိုင်နှုန်း (%) သင်္ကေတများဖြင့် ထည့်သွင်းပါမည်။ ဤလုပ်ဆောင်ချက်သည် "တစ်ဆင့်ပြီးတစ်ဆင့်" အလုပ်လုပ်သည်။ ပထမ % သင်္ကေတတွင်၊ arg1 ကိုထည့်သည်၊ ဒုတိယ % သင်္ကေတတွင်၊ arg2 ကိုထည့်သည် စသည်တို့ဖြစ်သည်။
မှတ်ချက်- အငြင်းအခုံများထက် % လက္ခဏာများ ပိုများနေပါက၊ နေရာကိုင်ဆောင်သူများကို အသုံးပြုရပါမည်။ % နိမိတ်လက္ခဏာပြီးနောက် နေရာယူထားသည်ကို ထည့်သွင်းပြီး အကြောင်းပြချက်- နံပါတ် နှင့် "\$" တို့ ပါဝင်ပါသည်။ ဥပမာနှစ်ခုကိုကြည့်ပါ။
အကြံပြုချက်- သက်ဆိုင်ရာ လုပ်ဆောင်ချက်များ- printf() ၊ vprintf() ၊ vsprintf() ၊ fprintf() နှင့် vfprintf()
အထားအသို
sprintf(format,arg1,arg2,arg++)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
format | Required. Specifies the string and how to format the variables in it. Possible format values:
Additional format values. These are placed between the % and the letter (example %.2f):
Note: If multiple additional format values are used, they must be in the same order as above. |
arg1 | Required. The argument to be inserted at the first %-sign in the format string |
arg2 | Optional. The argument to be inserted at the second %-sign in the format string |
arg++ | Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string |
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | ဖော်မတ်လုပ်ထားသည့် စာကြောင်းကို ပြန်ပေးသည်။ |
---|---|
PHP ဗားရှင်း- | 4+ |
နောက်ထပ် ဥပမာများ
ဥပမာ
ဖော်မတ်တန်ဖိုး %f ကိုအသုံးပြုခြင်း-
<?php
$number = 123;
$txt = sprintf("%f",$number);
echo $txt;
?>
ဥပမာ
နေရာထိုင်ခင်းအသုံးပြုမှု-
<?php
$number = 123;
$txt = sprintf("With 2 decimals: %1\$.2f
<br>With no decimals: %1\$u",$number);
echo $txt;
?>
ဥပမာ
ဖြစ်နိုင်သည့် ဖော်မတ်တန်ဖိုးများအားလုံးကို သရုပ်ပြခြင်း-
<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2
// Note: The format value "%%" returns a percent sign
echo sprintf("%%b = %b",$num1)."<br>"; // Binary number
echo sprintf("%%c = %c",$char)."<br>"; // The ASCII Character
echo sprintf("%%d = %d",$num1)."<br>"; // Signed decimal number
echo sprintf("%%d = %d",$num2)."<br>"; // Signed decimal number
echo sprintf("%%e = %e",$num1)."<br>"; // Scientific notation (lowercase)
echo sprintf("%%E = %E",$num1)."<br>"; // Scientific notation (uppercase)
echo sprintf("%%u = %u",$num1)."<br>"; // Unsigned decimal number (positive)
echo sprintf("%%u = %u",$num2)."<br>"; // Unsigned decimal number (negative)
echo sprintf("%%f = %f",$num1)."<br>"; // Floating-point number (local settings aware)
echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not local sett aware)
echo sprintf("%%g = %g",$num1)."<br>"; // Shorter of %e and %f
echo sprintf("%%G = %G",$num1)."<br>"; // Shorter of %E and %f
echo sprintf("%%o = %o",$num1)."<br>"; // Octal number
echo sprintf("%%s = %s",$num1)."<br>"; // String
echo sprintf("%%x = %x",$num1)."<br>"; // Hexadecimal number (lowercase)
echo sprintf("%%X = %X",$num1)."<br>"; // Hexadecimal number (uppercase)
echo sprintf("%%+d = %+d",$num1)."<br>"; // Sign specifier (positive)
echo sprintf("%%+d = %+d",$num2)."<br>"; // Sign specifier (negative)
?>
ဥပမာ
string specifiers ၏သရုပ်ပြမှု
<?php
$str1 = "Hello";
$str2 = "Hello world!";
echo sprintf("[%s]",$str1)."<br>";
echo sprintf("[%8s]",$str1)."<br>";
echo sprintf("[%-8s]",$str1)."<br>";
echo sprintf("[%08s]",$str1)."<br>";
echo sprintf("[%'*8s]",$str1)."<br>";
echo sprintf("[%8.8s]",$str2)."<br>";
?>
❮ PHP String အကိုးအကား