Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Makalah
  • Ke-NU-an
  • Kabar
  • Search
Menu

PHP Cookbook: Replacing Substrings

Posted on December 15, 2011

Problem
You want to replace a substring with a different string. For example, you want to obscure all but the last four digits of a credit card number before printing it.

Solution
Use substr_replace(), as in Example 1-15.
Example 1-15. Replacing a substring with substr_replace( )
[sourcecode language=”php”]
// Everything from position $start to the end of $old_string
// becomes $new_substring
$new_string = substr_replace($old_string,$new_substring,$start);
// $length characters, starting at position $start, become $new_substring
$new_string = substr_replace($old_string,$new_substring,$start,$length);
[/sourcecode]

Discussion
Without the $length argument, substr_replace( ) replaces everything from $start to the end of the string. If $length is specified, only that many characters are replaced:
[sourcecode language=”php”]
print substr_replace(‘My pet is a blue dog.’,’fish.’,12);
print substr_replace(‘My pet is a blue dog.’,’green’,12,4);
$credit_card = ‘4111 1111 1111 1111’;
print substr_replace($credit_card,’xxxx ‘,0,strlen($credit_card)-4);
[/sourcecode]
[sourcecode]
My pet is a fish.
My pet is a green dog.
xxxx 1111
[/sourcecode]

If $start is negative, the new substring is placed at $start characters counting from the end of $old_string, not from the beginning:
[sourcecode language=”php”]
print substr_replace(‘My pet is a blue dog.’,’fish.’,-9);
print substr_replace(‘My pet is a blue dog.’,’green’,-9,4);
[/sourcecode]
[sourcecode]
My pet is a fish.
My pet is a green dog.
[/sourcecode]

If $start and $length are 0, the new substring is inserted at the start of $old_string:
print substr_replace('My pet is a blue dog.','Title: ',0,0);
Title: My pet is a blue dog.

The function substr_replace( ) is useful when you’ve got text that’s too big to display all at once, and you want to display some of the text with a link to the rest. Example 1-16 displays the first 25 characters of a message with an ellipsis after it as a link to a page that displays more text.

Example 1-16. Displaying long text with an ellipsis
[sourcecode language=”php”]
$r = mysql_query("SELECT id,message FROM messages WHERE id = $id") or die();
$ob = mysql_fetch_object($r);
printf(‘<a href="more-text.php?id=%d">%s</a>’,
$ob->id, substr_replace($ob->message,’ …’,25));
[/sourcecode]

The more-text.php page referenced in Example 1-16 can use the message ID passed in the query string to retrieve the full message and display it.

See Also
Documentation on substr_replace( ) at http://www.php.net/substr-replace.

Terbaru

  • Google Tingkatkan Batas Gratis Gemini 3 Pro untuk Pengembang dan Bisnis
  • Google Perkenalkan ‘Circle to Search’: Cara Baru Menggunakan AI untuk Pencarian
  • OpenAI Terpapar Data Pelanggan Melalui Pelanggaran Vendor Mixpanel, API Terpengaruh
  • Error External Drive Extraction Tidak Terdeteksi di VM Virtual Hyper-V
  • Ringkasan Notifikasi Pixel Google Lebih Baik dari iPhone, Tapi Apa Tujuannya?
  • ShadowV2 Botnet Digunakan Uji Coba Setelah Penyebab Gangguan AWS
  • YouTube TV Segera Kembali Menawarkan Saluran Univision Setelah Penangguhan Dua Bulan
  • YouTube TV dan Disney Meluncurkan Penawaran Paket Bundel dengan Harga Menarik
  • Error Microsoft .NET Framework: Unhandled Exception – Not Enough Space On The Disk
  • Qualcomm Snapdragon 8 Gen 5: Chipset Flagship Baru yang Lebih Cepat dan Efisien
  • Serangan ONSolve Terus Mengganggu Sistem Peringatan Darurat di Seluruh Amerika Serikat
  • Android 16 Akan Membawa QPR2 untuk Launcher Pixel, Fokus pada Pengalaman Pengguna yang Ditingkatkan
  • Android 16 (QPR2) Akan Membawa Perubahan Signifikan pada Launcher Pixel
  • Microsoft Akan Tingkatkan Kinerja Microsoft Teams dengan Pengelola Panggilan Baru
  • Samsung Meluncurkan Diskon Besar-besaran untuk Penjualan Black Friday 2025, Berapa Harga Terbaru?
  • Samsung Black Friday Sale Phones and Rings Discounts – Huge Savings on Galaxy Devices
  • Error ‘Insufficient Permissions’ (Izin Tidak Cukup) Saat Membuka Email di Windows – Solusi dan Penjelasan Lengkap
  • Hadir dengan Refresh Rate 144 Hz! Ini Rekomendasi Laptop Gaming 10 Jutaan dari ASUS
  • Cara Memperbaiki Error ScanPST.exe dan File PST di Microsoft Outlook
  • Cara Mengatasi Error DWMAPI.DLL is either not designed or not found
  • Cara Memperbaiki Error Equation/Rumus Jelek di Microsoft Word
  • Cara Mengatasi Adapter Jaringan VMware yang Hilang di Windows 11
  • Cara Reset Multi-Factor Authentication (MFA) di Microsoft Entra
  • Cara Mengatasi Masalah Konektivitas VM Hyper-V ke Host
  • Cara Memperbaiki Error 0x8000FFFF Catastrophic Failure Saat Ekstrak Zip
  • Cara Memperbaiki File Explorer Crash Saat Membuka Folder Besar di Windows 11/10
  • Cara Mengatasi Error Login 0x8007003B di Outlook, Microsoft, XBox dll
  • Cara Memulihkan Akun Admin Microsoft 365 Karena MFA Gagal
  • Cara Mengatasi Error “A Conexant audio device could not be found”
  • Cara Memperbaiki Windows Tidak Nyala Lagi Setelah Sleep/Locked
  • Google Tingkatkan Batas Gratis Gemini 3 Pro untuk Pengembang dan Bisnis
  • Google Perkenalkan ‘Circle to Search’: Cara Baru Menggunakan AI untuk Pencarian
  • OpenAI Terpapar Data Pelanggan Melalui Pelanggaran Vendor Mixpanel, API Terpengaruh

©2025 emka.web.id | Design: Newspaperly WordPress Theme