PHP Replace Function 1. str_replace() Replace all occurrences of the search string with the replacement string 2. substr_replace() Replace text within a portion of a string 3. strtr() Translate characters or replace substrings 4. preg_replace() Perform a regular expression search and replace แต่ละฟังก์ชั่นแตกต่างกันอย่างไร เริ่มจาก => 1. str_replace() เรามักจะคุ้นเคยกับการใช้เพื่อแทนที่คำบางคำให้เป็นคำอื่น เช่น echo str_replace("ผศ.ดร.", "", "ผศ.ดร.สมบูรณ์ ใจดี"); ก็จะได้ผลลัพธ์ที่แทนที่ ผศ.ดร. เป็นค่าว่าง = สมบูรณ์ ใจดี หรือ ใช้ตัดเครื่องหมายคอมม่าในตัวเลขออกไป echo str_replace(",", "", "1,234,654.00"); ก็จะได้แต่ตัวเลข 1234654.00 เพื่อเอาไปบันทึกในฐานข้อมูล นอกจากนี้ยังสามารถนำไปใช้แทนที่คำหยาบได้ด้วย ยกตัวอย่าง $rough = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $c...
ความคิดเห็น
แสดงความคิดเห็น