PHP วิธีการแทนที่ตัวเลข 4 ตัวท้ายของเบอร์โทรศัพท์

โดย SONGCHAI SAETERN
วิธีการแทนที่ตัวเลข 4 ตัวท้ายของเบอร์โทรศัพท์

INPUT
0801234567

ผลลัพธ์ที่ต้องการ

080123xxxx

ตัวอย่างโค้ด PHP

<?php
    $tel_number = '0801234567';
    $new_num = substr_replace($tel_number, 'xxxx', -4); //080123xxxx 

    echo '<h3>ผลลัพธ์</h3>';
    echo $new_num;
?>



นอกจากใช้แทนที่เบอร์โทรศัพท์แล้ว ยังสามารถใช้แทนที่ข้อความอื่นๆ โดยกำหนดตำแหน่งต่างๆ ได้โดยสามารถอ่านรายละเอียดเพิ่มเติมได้ดังนี้


คำอธิบายฟังก์ชั่น

substr_replace

(PHP 4, PHP 5, PHP 7)
substr_replace — Replace text within a portion of a string

Description ¶

mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
substr_replace() replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.

อ่านเพิ่มเติม
http://php.net/manual/en/function.substr-replace.php