PHP htmlspecialchars() လုပ်ဆောင်ချက်
ဥပမာ
ကြိုတင်သတ်မှတ်ထားသော စာလုံး "<" (ထက်နည်းသော) နှင့် ">" (ထက်ကြီးသည်) ကို HTML entities သို့ ပြောင်းပါ-
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
အထက်ဖော်ပြပါ code ၏ HTML အထွက်သည် (View Source) ဖြစ်လိမ့်မည်။
<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
အထက်ဖော်ပြပါ ကုဒ်၏ ဘရောက်ဆာ၏ ထွက်ပေါက်မှာ-
This is some <b>bold</b> text.
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
htmlspecialchars() လုပ်ဆောင်ချက်သည် အချို့သော ကြိုတင်သတ်မှတ်ထားသော စာလုံးများကို HTML entities အဖြစ်သို့ ပြောင်းပေးသည်။
ကြိုတင်သတ်မှတ်ထားသော ဇာတ်ကောင်များမှာ-
- & (ampersand) ဖြစ်လာသည် &
- "(ကိုးကားနှစ်ချက်) သည် "
- ' (ကိုးကားချက်) သည် '
- < (အောက်) ဖြစ်သွားသည် <
- > (ထက်ကြီးသည်) >
အကြံပြုချက်- အထူး HTML အရာများကို ဇာတ်ကောင်များထံ ပြန်ပြောင်းရန်၊ htmlspecialchars_decode() လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။
အထားအသို
htmlspecialchars(string,flags,character-set,double_encode)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
string | Required. Specifies the string to convert |
flags | Optional. Specifies how to handle quotes, invalid encoding and the used document type. The available quote styles are:
Invalid encoding:
Additional flags for specifying the used doctype:
|
character-set | Optional. A string that specifies which character-set to use. Allowed values are:
Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8. |
double_encode | Optional. A boolean value that specifies whether to encode existing html entities or not.
|
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | ပြောင်းထားသောစာကြောင်း ကို ပြန်ပေး သည် |
---|---|
PHP ဗားရှင်း- | 4+ |
ပြောင်းလဲမှုမှတ်တမ်း- | PHP 5.6 - အက္ခရာ-အစုံ ပါရာမီတာ အတွက် မူရင်းတန်ဖိုးကို ပုံသေ စာလုံး
ရေ (ဖွဲ့စည်းမှုတွင်) တန်ဖိုးသို့ ပြောင်းလဲခဲ့သည်။ PHP 5.4 - ဇာတ်ကောင်သတ်မှတ်ထားသော ကန့ ်သတ်ဘောင်တန်ဖိုးကို UTF-8 သို့ ပြောင်းလဲခဲ့သည်။ PHP 5.4 - Added ENT_SUBSTITUTE၊ ENT_DISALLOWED၊ ENT_HTML401၊ ENT_HTML5၊ ENT_XML1 နှင့် ENT_XHTML PHP 5.3 - ထည့်ထားသော ENT_IGNORE ကိန်းသေ။ PHP 5.2.3 - double_encode ကန့်သတ်ဘောင်ကို ထည့်ထားသည်။ PHP 4.1 - ဇာတ်ကောင်သတ်မှတ်ထားသော ကန့်သတ် ဘောင်ကို ထည့် ထားသည်။ |
နောက်ထပ် ဥပမာများ
ဥပမာ
အချို့သော ကြိုတင်သတ်မှတ်ထားသော စာလုံးများကို HTML အကြောင်းအရာများသို့ ပြောင်းပါ-
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>
အထက်ဖော်ပြပါ code ၏ HTML အထွက်သည် (View Source) ဖြစ်လိမ့်မည်။
<!DOCTYPE html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
အထက်ဖော်ပြပါ ကုဒ်၏ ဘရောက်ဆာ၏ ထွက်ပေါက်မှာ-
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
ဥပမာ
ကိုးကားနှစ်ဆကို HTML အကြောင်းအရာသို့ ပြောင်းပါ-
<?php
$str = 'I love "PHP".';
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
?>
အထက်ဖော်ပြပါ code ၏ HTML အထွက်သည် (View Source) ဖြစ်လိမ့်မည်။
<!DOCTYPE html>
<html>
<body>
I love "PHP".
</body>
</html>
အထက်ဖော်ပြပါ ကုဒ်၏ ဘရောက်ဆာ၏ ထွက်ပေါက်မှာ-
I love "PHP".
❮ PHP String အကိုးအကား