Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • 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

  • Inilah Spesifikasi Vivo Y31d Pro, HP Murah dengan Baterai 7.000 mAh dan Bodi Tahan Banting Ekstrem
  • Inilah 5 Rekomendasi HP Samsung dengan Fitur NFC Terbaik yang Bikin Hidup Makin Praktis dan Satset
  • Inilah Deretan HP Vivo dengan Kamera Stabilizer Terbaik 2026, Cocok Banget Buat Konten Kreator Modal Minim!
  • Cara Mendapatkan Lead dan Rekomendasi SEO dari ChatGPT dan Claude (Update Tahun 2026)
  • Inilah Rekomendasi HP Infinix Kamera Bagus dan RAM Besar Paling Juara Buat Konten dan Gaming!
  • Inilah Trik Supaya Koneksi Indosat Makin Kencang, Tutorial Setting APN Terbaru 2026!
  • Inilah 7 HP Xiaomi NFC Termurah 2025, Dompet Aman Aktivitas Tetap Lancar!
  • Inilah 5 Rekomendasi HP Samsung dengan Fitur NFC Termurah dan Terbaik untuk Kebutuhan Harian Kalian
  • Inilah Rekomendasi HP Vivo Harga 2 Jutaan dengan Fitur Stabilizer Kamera Terbaik untuk Konten Kreator
  • Inilah 5 HP Infinix Kamera Bagus dan RAM Besar, Bikin Konten Jadi Makin Estetik!
  • Inilah Alasan Kenapa Lenovo Yoga Tab Bakal Jadi Tablet AI Paling Gahar di Tahun 2026
  • Inilah Bocoran Tecno Pova 8 dan Spark 50 Pro, HP dengan Baterai Monster Sampai 7.750mAh!
  • Inilah Alasan Kenapa Poco X8 Pro Series Ludes Terjual 30 Ribu Unit dalam Sehari, Performanya Benar-Benar Naik Kelas!
  • Inilah Rekomendasi HP Samsung dengan Kamera Terbaik 2025, Hasil Foto Dijamin Kayak Profesional!
  • Inilah Kemudahan Belanja Elektronik Lewat Kolaborasi Strategis Indodana Finance dan Sharp Indonesia
  • Inilah Rekomendasi Smartwatch Mirip Apple Watch Termurah 2026 yang Bikin Gaya Makin Maksimal
  • Inilah Cara Cek Lokasi UTBK 2026 Agar Tidak Salah Alamat dan Terlambat
  • Inilah Realme Narzo 100 Lite 5G, Smartphone Baterai 7000mAh yang Siap Meluncur dengan Spesifikasi Gahar dan Layar Super Smooth
  • Inilah Alasan Kenapa Aplikasi MOVA Berbahaya dan Bukan Cara Cepat Kaya yang Aman
  • Inilah Huawei Watch GT Runner 2, Smartwatch Keren yang Siap Bikin Lari Kalian Makin Kencang dan Presisi!
  • Inilah Panduan Lengkap Mengunduh dan Mencetak Kartu Peserta UTBK 2026 Biar Nggak Salah Langkah
  • Apa itu Satgas PKH? Tim Khusus yang Bakal Sikat Penguasaan Hutan Ilegal di Indonesia
  • Inilah REDMI Pad 2 SE, Tablet Murah dengan Layar 2K yang Siap Bikin Nyaman Mata Kalian
  • Apa itu msgstore.db.crypt14 di WhatsApp? Jangan Asal Hapus Kalau Nggak Mau Chat Hilang!
  • Inilah Fakta di Balik Kasus Siswi 15 Tahun di Langkat yang Viral Jadi Tersangka Usai Bela Ayahnya
  • Inilah 7 HP Redmi Kamera Terbaik 2026 dengan Resolusi 200 MP, Kualitas Flagship Harga Tetap Irit!
  • Inilah Bahaya dan Cara Kerja Unlock FF Beta Server Account Apk Mobilitado yang Lagi Viral
  • Inilah 5 Laptop Lenovo Paling Awet dan Tangguh Buat Investasi Jangka Panjang Kalian
  • Inilah Kronologi Tragis Mahasiswa PNP Padang Ditemukan Meninggal di Kamar Kos 11 April 2026
  • Inilah Kenapa Akun WhatsApp Kalian Sedang Ditinjau dan Cara Mengatasinya Biar Normal Lagi
  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to use Gamma AI as a complete design engine for professional presentations and graphics
  • A beginner tutorial on the Claude Advisor: Learn how to boost your AI agent’s intelligence with the advisor strategy
  • Complete tutorial on building a powerful AI knowledge graph with Obsidian and Claude Code
  • Complete Tutorial: How to Master New Google Gemini’s New Interactive 3D Visualizations and Simulations
  • How to Coordinate Multiple AI Agents with the Claude Teams Observer Pattern: A Step-by-Step Tutorial
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

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