Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

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

PHP Cookbook: Extracting Substrings

Posted on December 11, 2011

Problem
You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.

Solution
Use substr( ) to select your substring, as in Example 1-9. Example 1-9. Extracting a substring with substr( )
[sourcecode language=”php”]
<?php
$substring = substr($string,$start,$length);
$username = substr($_GET[‘username’],0,8);
?>
[/sourcecode]

Discussion
If $start and $length are positive, substr( ) returns $length characters in the string, starting at $start. The first character in the string is at position 0. Example 1-10 has positive $start and $length.

[sourcecode language=”php”]print substr(‘watch out for that tree’,6,5);[/sourcecode]

Example 1-10 prints:
out f

If you leave out $length, substr( ) returns the string from $start to the end of the original string, as shown in Example 1-11.

Example 1-11. Using substr( ) with positive start and no length
[sourcecode language=”php”]print substr(‘watch out for that tree’,17);[/sourcecode]

Example 1-11 prints:
t tree

If $start is bigger than the length of the string, substr( ) returns false..
If $start plus $length goes past the end of the string, substr( ) returns all of the string from $start forward, as shown in Example 1-12.

Example 1-12. Using substr( ) with length past the end of the string
[sourcecode language=”php”]print substr(‘watch out for that tree’,20,5);[/sourcecode]

Example 1-12 prints:
ree

If $start is negative, substr( ) counts back from the end of the string to determine where your substring starts, as shown in Example 1-13.

Example 1-13. Using substr( ) with negative start
[sourcecode language=”php”]
print substr(‘watch out for that tree’,-6);
print substr(‘watch out for that tree’,-17,5);
[/sourcecode]

Example 1-13 prints:
t tree
out f

With a negative $start value that goes past the beginning of the string (for example, if $start is −27 with a 20-character string), substr( ) behaves as if $start is 0.

If $length is negative, substr( ) counts back from the end of the string to determine where your substring ends, as shown in Example 1-14.

Example 1-14. Using substr( ) with negative length
[sourcecode language=”php”]
print substr(‘watch out for that tree’,15,-2);
print substr(‘watch out for that tree’,-4,-1);
[/sourcecode]

Example 1-14 prints:

hat tr
tre

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

Terbaru

  • Google Fi Mendukung Panggilan Telepon RCS Melalui Web, Lebih Mudah dan Efisien
  • Data Breach Marquis: Hajar Lebih Dari 74 Bank dan Koperasi AS
  • Google Search Akan Adopsi ‘Continuous Circle’ untuk Hasil Pencarian Terjemahan, Lebih Cerdas dan Kontekstual
  • Rusia Memblokir Roblox Karena Distribusi ‘Propaganda LGBT’
  • Google Gemini Redesain Web Total di Desember 2025, Fokus UX yang Lebih Baik
  • Apa itu Google Workspace Studio? Tool Baru untuk Pembuat Konten?
  • Cara Menggunakan Xbox Full-Screen Experience di Windows
  • Korea Tahan Tersangka Terkait Penjualan Video Intim dari Kamera CCTV yang Diretas
  • Kebocoran Galaxy Buds 4 Mengungkap Desain dan Fitur Baru, Mirip Apple?
  • Sudah Update Windows KB5070311 dan Apa Saja Yang Diperbaiki?
  • Cara Menonaktifkan Fitur AI Actions (Tindakan AI) di Menu Windows Explorer
  • Microsoft Edge AI vs. OpenAI’s Atlas Browser: Perbandingan dan Perbedaan Utama
  • Cara Memasang Folder Sebagai Drive di Windows 11
  • Cara Memperbaiki Error 0xC1900101 0x40021 pada Update Windows 11
  • Malware Glassworm Serang Lagi VSCode, Hati-hati!
  • Walmart dan Google Bermitra untuk Kamera Rumah Google Home: Pengalaman Langsung
  • Gemini Dapat Bisa Atur Perangkat Rumah Melalui Home Assistant Pakai Suara, Desember 2025
  • Asahi, Produsen Bir Jepang, Akui Kebocoran Data 15 Juta Pelanggan
  • Google Messages Ada Fitur Baru: Pesan Grup, Mode Gelap dan Integrasi dengan Google Duo
  • 5 Laptop ASUS Terbaik dengan Tampilan Mewah dan Build Quality Premium
  • Pria di Balik Serangan ‘Twin Wifi’ Mencuri Wifi, Dikenakan Hukuman 7 Tahun Penjara
  • Google Kembangkan Fitur Baru untuk Tugas di Keep, Lebih Terintegrasi dengan Kalender
  • Google Akan Luncurkan Laptop dan Ponsel Android Baru di Tahun 2025: Murah & Spesifikasi Tinggi
  • Samsung Galaxy Z-Fold Tri-Fold: Harga dan Spesifikasi Resmi Terungkap
  • Federasi Sepak Bola Prancis (FFF) Mengungkap Pelanggaran Data Setelah Serangan Siber
  • Google Perbarui Desain Akun Google Web, Fokus pada Kemudahan Penggunaan dan Integrasi
  • 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
  • Google Fi Mendukung Panggilan Telepon RCS Melalui Web, Lebih Mudah dan Efisien
  • Data Breach Marquis: Hajar Lebih Dari 74 Bank dan Koperasi AS
  • Google Search Akan Adopsi ‘Continuous Circle’ untuk Hasil Pencarian Terjemahan, Lebih Cerdas dan Kontekstual

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