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 Cara Menilai Sumbangan yang Disetarakan dengan Uang Supaya Pajak Kalian Berkurang
  • Apa itu Pin di iMessage?
  • SKTP Nggak Muncul di Info GTK padahal Sudah Terbit? Ini Trik Rahasia Biar Data Langsung Update!
  • Ini Trik Nuyul Cari Cuan di Game Puzzle Farm 2026 Biar Koin Melimpah Tanpa Undang Teman
  • Inilah Ukuran Kertas Thermal 58mm ISO Di Word, Berapa dan Panduan Lengkap Memilihnya
  • Bukan Cuma Zakat! Ternyata Sumbangan Jenis Ini Bisa Ngurangin Pajak Kalian! Simak Penjelasannya
  • Inilah Caranya Mengajar Bahasa Indonesia di Amerika Serikat Lewat Beasiswa Fulbright FLTA 2026
  • Inilah 6 Rekomendasi HP yang Awet dan Tahan Lama Biar Kalian Nggak Gonta-ganti Terus!
  • Apa itu Proses BOP dan Psikotes BRI Life?
  • Ini Cara Input Tugas Tambahan Guru di EMIS GTK IMP 2026 Biar Jam Mengajar Aman!
  • APK Juice Pack Frenzy Penipuan? Benarkah Membayar atau Cuma Tipuan Iklan? Ini Faktanya!
  • Apakah Apk ReelAct Penipu? Mau Tarik 100 Dolar dari Reel Act? Cek Dulu Faktanya Biar Nggak Rugi Waktu!
  • Inilah Rekomendasi Game Turn Base Android dan PC Terbaik Buat Kalian yang Suka Strategi!
  • Inilah Cara Membuat Sertifikat di Canva dan Ukuran Standar yang Wajib Kalian Tahu
  • Inilah Aturan Zakat yang Bisa Jadi Pengurang Pajak Bruto Kalian, Sudah Tahu Belum?
  • Inilah Data Pendaftar KIP Kuliah 2026 Jalur SNBP dan Bocoran Kriteria yang Lolos!
  • Inilah Game Silent Hill: Townfall, Teror Psikologis Baru yang Bakal Bikin Kalian Gemetar di Tahun 2026!
  • Inilah Trailer Mortal Kombat 2, Johnny Cage Resmi Gabung dan Siap Hadapi Shao Kahn!
  • Inilah Spesifikasi Lengkap Samsung Galaxy S26 yang Baru Meluncur, Ternyata Harganya Naik Segini!
  • Inilah Cara Mematikan MSA Xiaomi Supaya HP Nggak Lemot dan Bebas Iklan, Ternyata Langkahnya Simpel Banget!
  • Inilah Kronologi Mobil Calya Plat D yang Viral Lawan Arus di Jakpus, Ternyata Bawa Banyak Plat Palsu!
  • Inilah Axioo Hype AI 5, Laptop AI Canggih yang Nggak Bikin Kantong Bolong!
  • Ini Loh Kejahatan Modus Phishing Google Tasks Terbaru yang Bisa Nguras Data Perusahaan Kalian
  • Inilah Poco X7 5G dan M7 Pro 5G, HP Gaming Performa Ekstrem yang Ngebikin Mabar Kalian Jadi Anti Lag!
  • Inilah Cara Mengubah Lahan Kosong Jadi Uang Lewat Strategi Land Banking
  • Ini Trik Supaya Gajian YouTube Shorts Tembus Puluhan Juta dari Penonton Bule!
  • Jangan Sampai Keliru! Begini Cara Cek Total Jam Linear dan Non Linear di EMIS GTK IMP 2026
  • Cuma Nonton Drama Pendek Bisa Cair Uang Tunai? Bongkar Habis Kebenaran Aplikasi FunFlick di Sini!
  • Apa itu Resetter Epson L3210?
  • Ini Loh Ukuran A4, F4, A3, B5, A5 di Canva Biar Hasil Cetakan Kalian Nggak Terpotong!
  • Ubuntu 26.04 LTS Snapshot 4 Release Notes and Download Guide
  • What’s New in Calibre 9.4? A Deep Dive into the Latest E-Book Progress Tracking Features
  •  How to Fix Clipchamp No Internet Connection Error on Windows
  • Why Are Sudo Password Asterisks Missing in Ubuntu 26.04?
  • Omarchy Linux 3.4 Released: Arch-Based OS with New Features Explained
  • Prompt AI Menyusun Script Pola Suara Karakter agar Brand Jadi Ikonik
  • Prompt AI untuk Merancang Karakter Brand yang Ikonik
  • Prompt AI Audit Konten Sesuai Karakter Brand
  • Prompt AI Merubah Postingan LinkedIn Jadi Ladang Diskusi dengan ChatGPT
  • Prompt AI: Paksa Algoritma LinkedIn Promosikan Konten Kalian
  • 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
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ

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