PHP 5 String Functions ฟังค์ชันทางด้านข้อความของภาษา PHP

19 ส.ค. 2016 , 21,338 Views   , หมวดหมู่ PHP ทุกหมวดหมู่ โค๊ดดิ้ง   ,


php_string_functions

PHP 5 String Functions

ฟังค์ชันทางด้านข้อความของภาษา PHP

ฟังก์ชั่น รายละเอียด
addcslashes() ใส่ backslashes (\) ด้านหน้าตัวอักษรที่ต้องการ
$str = addcslashes(“Hello World!”,”d”);
echo($str);
ผลลัพธ์ : Hello Worl\d!
addslashes() ใส่ backslashes (\) ด้านหน้า อักขระพิเศษ (predefined characters)
The predefined characters are:

  • single quote (‘)
  • double quote (“)
  • backslash (\)
  • NULL
bin2hex() Converts a string of ASCII characters to hexadecimal values
chop() ตัดช่องว่างท้ายข้อความออกไป
chr() แปลงค่า ASCII เป็นตัวอักษร
chunk_split() แยกข้อความโดยคั่นด้วยข้อความที่ต้องการ เช่น Hello world! เป็น He.ll.o .wo.rl.d!. เป็นต้น
convert_cyr_string() Converts a string from one Cyrillic character-set to another
convert_uudecode() Decodes a uuencoded string
convert_uuencode() Encodes a string using the uuencode algorithm
count_chars() Returns information about characters used in a string
crc32() Calculates a 32-bit CRC for a string
crypt() One-way string hashing
echo() แสดงผลข้อความ
explode() แยกข้อความโดยใช้เครื่องหมายแยก ออกมาเก็บเป็น array
fprintf() Writes a formatted string to a specified output stream
get_html_translation_table() Returns the translation table used by htmlspecialchars()
and htmlentities() Default : HTML_SPECIALCHARS
เช่น
print_r (get_html_translation_table());
Array
(
[“] => "
[<] => &lt;
[>] => &gt;
[&] => &amp;
)
print_r (get_html_translation_table(HTML_ENTITIES));
Array
(
[ ] => &nbsp;
[¡] => &iexcl;
[¢] => &cent;
[£] => &pound;
ฯลฯ
เป็นต้น
hebrev() Converts Hebrew text to visual text
hebrevc() Converts Hebrew text to visual text and new lines (\n) into <br>
hex2bin() Converts a string of hexadecimal values to ASCII characters
html_entity_decode() Converts HTML entities to characters
htmlentities() Converts characters to HTML entities
htmlspecialchars_decode() เช่น จาก &lt;strong&gt;ทดสอบนะครับ&lt;/strong&gt; มาเป็น ทดสอบนะครับ เป็นต้น
htmlspecialchars() แสดง tag html
เช่น แปลงจาก ทดสอบนะครับ มาเป็น <strong>ทดสอบนะครับ</strong> เป็นต้น
implode() รวม array เป็นข้อความ
join() รวม array เป็นข้อความ Alias of implode()
lcfirst() แปลงตัวอักษรตัวแรกเป็นตัวพิมพ์เล็ก
levenshtein() Returns the Levenshtein distance between two strings
localeconv() Returns locale numeric and monetary formatting information
ltrim() ตัดช่องว่างหรือตัวอักษรที่กำหนดทางด้านซ้ายออก (ด้านหน้าข้อความ)
md5() Calculates the MD5 hash of a string
md5_file() Calculates the MD5 hash of a file
metaphone() Calculates the metaphone key of a string
money_format() แสดงฟอร์แมตสกุลเงิน (Returns a string formatted as a currency string)
nl_langinfo() Returns specific local information
nl2br() ขึ้นบรรทัดใหม่ (<br> or <br />) ในข้อความที่มีการ enter เพื่อขึ้นบรรทัดใหม่ (\ n)
number_format() แสดงรูปแบบของตัวเลข
ord() Returns the ASCII value of the first character of a string
parse_str() ผ่านค่า Query String เข้าไปในตัวแปล
parse_str(“name=Peter&age=43″);
echo $name.”,”.$age;
ผลลัพธ์ : Peter,43
หมายเหตุ $_SERVER[‘QUERY_STRING’] ฟังค์ชันสำหรับ Get query string
print() แสดงผลข้อความ
printf() แสดงผลข้อความแบบมีรูปแบบ เช่น
$number = 9;
$str = “Beijing”;
printf(“There are %u million bicycles in %s.”,$number,$str);
ผลลัพธ์ : There are 9 million bicycles in Beijing. เป็นต้น
quoted_printable_decode() Converts a quoted-printable string to an 8-bit string
quoted_printable_encode() Converts an 8-bit string to a quoted printable string
quotemeta() Quotes meta characters
rtrim() ตัดช่องว่างหรือตัวอักษรที่กำหนดทางด้านขวาออก (ด้านหลังข้อความ)
setlocale() Sets locale information
sha1() Calculates the SHA-1 hash of a string
sha1_file() Calculates the SHA-1 hash of a file
similar_text() Calculates the similarity between two strings
soundex() Calculates the soundex key of a string
sprintf() Writes a formatted string to a variable
sscanf() Parses input from a string according to a format
str_getcsv() Parses a CSV string into an array
str_ireplace() เปลี่ยนข้อความที่พบด้วยคำที่ต้องการ (case-insensitive)
str_pad() เติมอักษรเข้าข้อความด้วยคำที่ต้องการ (กำหนดจำนวนได้)
$str = “Hello World”;
echo str_pad($str,20,”.”);
ผลลัพธ์ : Hello World………
str_repeat() สร้างข้อความซ้ำๆด้วยคำที่ต้องการ พร้อมระบุจำนวนครั้งที่ซ้ำ
str_replace() เปลี่ยนข้อความที่พบด้วยคำที่ต้องการ (case-sensitive)
str_rot13() Performs the ROT13 encoding on a string
str_shuffle() Random สุ่มตัวอักษร เช่น echo str_shuffle(“Hello World”); ผลลัพธ์ : ldor lHWoel เป็นต้น
str_split() แยกข้อความเก็บใน array
เช่น print_r(str_split(“Hello”,3));
ผลลัพธ์ : Array ( [0] => Hel [1] => lo ) เป็นต้น
str_word_count() นับจำนวนคำที่มีอยู่ในข้อความ เช่น str_word_count(“Hello world!”); ผลลัพธ์ : 2 เป็นต้น
strcasecmp() Compares two strings (case-insensitive)
strchr() ตัดข้อความบางส่วนตั้งแต่ตัวแรกที่พบจนถึงท้ายสุดเพื่อนำมาใช้ (alias of strstr())
strcmp() Compares two strings (case-sensitive)
strcoll() Compares two strings (locale based string comparison)
strcspn() นับจำนวนตัวอักษร(รวมช่องว่าง)ก่อนถึงข้อความที่กำหนด
เช่น echo strcspn(“Hello world!”,”w”); ผลลัพธ์ : 6
strip_tags() ตัด Tag HTML, XML, PHP ออกจากข้อความ
stripcslashes() นำ backslash (\) ออกจากข้อความ (Unquotes a string quoted with addcslashes())
stripslashes() นำ backslash (\) ออกจากข้อความ (Unquotes a string quoted with addslashes())
stripos() หาตำแหน่งแรกของข้อความที่ต้องการ (case-insensitive)
stristr() ตัดข้อความบางส่วนตั้งแต่ตัวแรกที่พบจนถึงท้ายสุดเพื่อนำมาใช้
(case-insensitive พิมพ์ใหญ่พิมพ์เล็กไม่มีผล)
เช่น
echo stristr(“Hello world!”,”world”); ผลลัพธ์ world!
echo stristr(“Hello world!”,”world”,true); ผลลัพธ์ Hello
เป็นต้น
strlen() ขนาดความยาวของข้อความ
strnatcasecmp() Compares two strings using a “natural order” algorithm (case-insensitive)
strnatcmp() Compares two strings using a “natural order” algorithm (case-sensitive)
strncasecmp() String comparison of the first n characters (case-insensitive)
strncmp() String comparison of the first n characters (case-sensitive)
strpbrk() Searches a string for any of a set of characters
strpos() หาตำแหน่งแรกของข้อความที่ต้องการ (case-sensitive พิมพ์ใหญ่พิมพ์เล็กมีผล)
strrchr() ตัดข้อความบางส่วนตั้งแต่ตัวสุดท้ายที่พบจนถึงท้ายสุดเพื่อนำมาใช้งาน
strrev() กลับข้อความ เช่น Hello World! เป็น !dlroW olleH เป็นต้น
strripos() หาตำแหน่งสุดท้ายของข้อความที่ต้องการ (case-insensitive พิมพ์ใหญ่พิมพ์เล็กไม่มีผล)
strrpos() หาตำแหน่งสุดท้ายของข้อความที่ต้องการ (case-sensitive พิมพ์ใหญ่พิมพ์เล็กมีผล)
strspn() Returns the number of characters found in a string
that contains only characters from a specified charlist
strstr() ตัดข้อความบางส่วนตั้งแต่ตัวแรกที่พบจนถึงท้ายสุดเพื่อนำมาใช้
(case-sensitive พิมพ์ใหญ่พิมพ์เล็กมีผล)
เช่น
echo strstr(“Hello world!”,”world”); ผลลัพธ์ world!
echo strstr(“Hello world!”,”world”,true); ผลลัพธ์ Hello
เป็นต้น
strtok() แยกข้อความด้วยคำที่ต้องการมาเก็บในตัวแปรที่ไม่ใช่
array (splits a string into smaller strings)
$string = “Hello world. Beautiful day today.”;
$token = strtok($string, ” “);
while ($token !== false) {
echo “$token,”;
$token = strtok(” “);
}
ผลลัพธ์ : Hello,world.,Beautiful,day,today.,
strtolower() ทำให้ข้อความเป็นตัวพิมพ์เล็ก
strtoupper() ทำให้ข้อความเป็นตัวพิมพ์ใหญ่
strtr() แปลหรือเปลี่ยนข้อความ ด้วยคำที่ต้องการ
เช่น $arr = array(“Hello” => “Hi”, “world” => “earth”);
echo strtr(“Hello world”,$arr); ผลลัพธ์เป็น Hi earth เป็นต้น
substr() ตัดตัวอักษรที่ต้องการใช้ออกมา
substr_compare() เปรียบเทียบสองข้อความ (binary safe and optionally case-sensitive)
ค่า return
0 – ถ้าสองสตริงเหมือนกัน
<0 – ถ้า string1 น้อยกว่า string2
>0 – ถ้า string1 มากกว่า string2
substr_count() นับจำนวนคำที่ต้องการ ว่าเกิดขึ้นกี่ครั้งในข้อความ
echo substr_count(“Hello world. The world is nice”,”world”);
ผลลัพธ์ : 2
substr_replace() แทนที่ข้อความ ด้วยข้อความที่่ต้องการ
trim() ตัดช่องว่างหรือตัวอักษรที่กำหนดทางด้านซ้ายและขวาออก
ucfirst() เปลี่ยนอักษรตัวแรกของข้อความเป็นตัวพิมพ์ใหญ่
ucwords() เปลี่ยนอักษรตัวแรกของแต่ละคำในข้อความเป็นตัวพิมพ์ใหญ่
vfprintf() Writes a formatted string to a specified output stream
vprintf() แสดงผลข้อความแบบมีรูปแบบ (รูปแบบเก็บอยู่ใน array)
vsprintf() กำหนดค่า ข้อความแบบมีรูปแบบ (รูปแบบเก็บอยู่ใน array) ใว้ในตัวแปล เช่น
$number = 9;
$str = “Beijing”;
$txt = vsprintf(“There are %u million bicycles in %s.”,array($number,$str));
echo $txt; เป็นต้น
wordwrap() ตัดข้อความเพื่อขึ้นบรรทัดใหม่ ด้วยเงื่อนไขความยาวของข้อความ
$str = “An example of a long word is: Supercalifragulistic”;
echo wordwrap($str,15,”<br>\n”);
ผลลัพธ์
An example of a
long word is:
Supercalifragulistic

ดูเพิ่มเติม
http://php.net/manual/en/ref.strings.php
http://www.w3schools.com/php/php_ref_string.asp