การจัดการ String: str_replace, strtoupper, strtolower
ในบทเรียนที่แล้ว เราได้เรียนรู้ฟังก์ชันพื้นฐานในการจัดการ String ใน PHP เช่น strlen(), strpos(), และ substr() ในบทนี้ เราจะมาเรียนรู้ฟังก์ชันเพิ่มเติมที่จะช่วยให้เราจัดการ String ได้หลากหลายยิ่งขึ้น ได้แก่ str_replace(), strtoupper(), และ strtolower()
การแทนที่ String ด้วย str_replace()
ฟังก์ชัน str_replace() ใช้สำหรับแทนที่ String บางส่วนใน String หลักด้วย String ที่เราต้องการ ไวยากรณ์ของฟังก์ชันนี้คือ:
str_replace(mixed $search, mixed $replace, mixed $subject, int &$count = null): mixed
โดยที่:
$search: String หรือ Array ของ String ที่ต้องการค้นหา$replace: String หรือ Array ของ String ที่จะนำมาแทนที่$subject: String หรือ Array ของ String ที่ต้องการค้นหาและแทนที่$count(optional): ตัวแปรที่รับค่าจำนวนครั้งที่ทำการแทนที่
ตัวอย่างที่ 1: แทนที่คำว่า "world" ด้วย "PHP"
<?php
$str = "Hello world!";
$newStr = str_replace("world", "PHP", $str);
echo $newStr; // Output: Hello PHP!
?>
ตัวอย่างที่ 2: แทนที่หลายคำพร้อมกันโดยใช้ Array
<?php
$str = "This is a test string.";
$search = array("is", "string");
$replace = array("was", "text");
$newStr = str_replace($search, $replace, $str);
echo $newStr; // Output: Thwas a test text.
?>
ตัวอย่างที่ 3: นับจำนวนครั้งที่ทำการแทนที่
<?php
$str = "Hello world! world!";
$newStr = str_replace("world", "PHP", $str, $count);
echo $newStr; // Output: Hello PHP! PHP!
echo "<br>";
echo "จำนวนครั้งที่แทนที่: " . $count; // Output: จำนวนครั้งที่แทนที่: 2
?>
ข้อควรระวัง: str_replace() เป็น case-sensitive (คำนึงถึงตัวพิมพ์เล็กตัวพิมพ์ใหญ่) หากต้องการแทนที่ไม่คำนึงถึงตัวพิมพ์เล็กตัวพิมพ์ใหญ่ สามารถใช้ฟังก์ชัน str_ireplace() แทนได้
การแปลง String เป็นตัวพิมพ์ใหญ่ด้วย strtoupper()
ฟังก์ชัน strtoupper() ใช้สำหรับแปลง String ทั้งหมดเป็นตัวพิมพ์ใหญ่ ไวยากรณ์ของฟังก์ชันนี้คือ:
strtoupper(string $string): string
โดยที่:
$string: String ที่ต้องการแปลงเป็นตัวพิมพ์ใหญ่
ตัวอย่าง:
<?php
$str = "hello world!";
$upperStr = strtoupper($str);
echo $upperStr; // Output: HELLO WORLD!
?>
ข้อควรระวัง: strtoupper() จะแปลงเฉพาะตัวอักษรภาษาอังกฤษเท่านั้น ตัวอักษรอื่นๆ เช่น ตัวเลข หรือสัญลักษณ์พิเศษ จะไม่ถูกเปลี่ยนแปลง
การแปลง String เป็นตัวพิมพ์เล็กด้วย strtolower()
ฟังก์ชัน strtolower() ใช้สำหรับแปลง String ทั้งหมดเป็นตัวพิมพ์เล็ก ไวยากรณ์ของฟังก์ชันนี้คือ:
strtolower(string $string): string
โดยที่:
$string: String ที่ต้องการแปลงเป็นตัวพิมพ์เล็ก
ตัวอย่าง:
<?php
$str = "HELLO WORLD!";
$lowerStr = strtolower($str);
echo $lowerStr; // Output: hello world!
?>
ข้อควรระวัง: เช่นเดียวกับ strtoupper(), strtolower() จะแปลงเฉพาะตัวอักษรภาษาอังกฤษเท่านั้น
สรุป
ในบทนี้ เราได้เรียนรู้ฟังก์ชัน str_replace(), strtoupper(), และ strtolower() ซึ่งเป็นฟังก์ชันที่มีประโยชน์ในการจัดการ String ใน PHP ฟังก์ชันเหล่านี้ช่วยให้เราสามารถแทนที่ String, แปลง String เป็นตัวพิมพ์ใหญ่ หรือแปลง String เป็นตัวพิมพ์เล็ก ได้อย่างง่ายดาย
ตารางสรุปฟังก์ชัน
| ฟังก์ชัน | คำอธิบาย | ตัวอย่าง |
|---|---|---|
str_replace() |
แทนที่ String บางส่วนใน String หลัก | str_replace("old", "new", "This is old.") จะได้ "This is new." |
strtoupper() |
แปลง String ทั้งหมดเป็นตัวพิมพ์ใหญ่ | strtoupper("hello") จะได้ "HELLO" |
strtolower() |
แปลง String ทั้งหมดเป็นตัวพิมพ์เล็ก | strtolower("WORLD") จะได้ "world" |
ในบทถัดไป เราจะมาเรียนรู้เกี่ยวกับ Superglobals ใน PHP ซึ่งเป็นตัวแปรพิเศษที่สามารถเข้าถึงได้จากทุกที่ใน Script ของเรา เริ่มจาก $_GET