PHP htmlentities() လုပ်ဆောင်ချက်
ဥပမာ
အချို့သော စာလုံးများကို HTML အကြောင်းအရာများသို့ ပြောင်းပါ-
<?php
$str = '<a href="https://www.w3schools.com">Go to
w3schools.com</a>';
echo htmlentities($str);
?>
အထက်ဖော်ပြပါ code ၏ HTML အထွက်သည် (View Source) ဖြစ်လိမ့်မည်။
<a href="https://www.w3schools.com">Go to w3schools.com</a>
အထက်ဖော်ပြပါ ကုဒ်၏ ဘရောက်ဆာ၏ ထွက်ပေါက်သည်-
<a href="https://www.w3schools.com">Go to w3schools.com</a>
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
htmlentities() function သည် စာလုံးများကို HTML entities သို့ ပြောင်းပေးသည်။
အကြံပြုချက်- HTML အကြောင်းအရာများကို ဇာတ်ကောင်အဖြစ်သို့ ပြန်ပြောင်းရန်၊ html_entity_decode() လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။
အကြံပြုချက်- htmlentities() မှအသုံးပြုသော ဘာသာပြန်ဇယားကို ပြန်ပေးရန်အတွက် get_html_translation_table () လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။
အထားအသို
htmlentities(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.
|
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | ပြောင်းထားသော စာကြောင်းကို ပြန်ပေးသည်။ သို့ရာတွင်၊ စာကြောင်း ပါရာမီတာတွင် မမှန်ကန်သောကုဒ်ထည့်ခြင်းပါ၀င်ပါက၊ ENT_IGNORE သို့မဟုတ် ENT_SUBSTITUTE အလံများကို သတ်မှတ်မထားပါက ၎င်းသည် အလွတ်စာကြောင်းတစ်ခု ပြန်ပေးလိမ့်မည်၊ |
---|---|
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 = "Albert Einstein said: 'E=MC²'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
?>
အထက်ဖော်ပြပါ code ၏ HTML အထွက်သည် (View Source) ဖြစ်လိမ့်မည်။
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said:
'E=MC²'<br>
Albert Einstein said: 'E=MC²'
အထက်ဖော်ပြပါ ကုဒ်၏ ဘရောက်ဆာ၏ ထွက်ပေါက်သည်-
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert
Einstein said: 'E=MC²'
ဥပမာ
အနောက်ဥရောပ စာလုံးအစုံကို အသုံးပြု၍ အချို့သော စာလုံးများကို HTML အကြောင်းအရာများအဖြစ် ပြောင်းပါ-
<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European
?>
အထက်ဖော်ပြပါ code ၏ HTML အထွက်သည် (View Source) ဖြစ်လိမ့်မည်။
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
အထက်ဖော်ပြပါ ကုဒ်၏ ဘရောက်ဆာ၏ ထွက်ပေါက်သည်-
My name is Øyvind Åsane. I'm Norwegian.
❮ PHP String အကိုးအကား