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

  • Cara Menggunakan Stellarium Web
  • Cara Menghapus Data KTP Pribadi di Pinjol yang Belum Lunas
  • Cara Mengganti Nomor TikTok yang Tidak Aktif atau Hilang Tanpa Verifikasi
  • Cara Menggunakan BCA PayLater Terbaru 2025
  • Cara Mendapatkan IMPoint Indosat IM3 Ooredoo Gratis via MyIM3
  • Apa Arti TikTok ‘Shared With You’?
  • Cara Menghapus Data KTP di Pinjol: Panduan Lengkap
  • Cara Download WhatsApp GB Terbaru 2025 – Fitur Lengkap & Aman
  • Review WhatsApp Beta: Apakah Aman? Cara Instal dan Cara Keluar
  • Bebong: Makna, Asal Usul, dan Penggunaan dalam Bahasa Indonesia
  • Spinjam dan Spaylater: Apa yang Terjadi Jika Terlambat Membayar dan Bisakah Meminjam Lagi?
  • Cara Download dan Menonton Dood Stream Tanpa Iklan – Doods Pro
  • Cara Menghentikan dan Mengatasi Pinjol Ilegal
  • Kode Bank BRI untuk Transfer ke PayPal
  • Cara Menyadap WhatsApp Tanpa Aplikasi dan Kode QR
  • Apa yang Terjadi Jika Telat Bayar Shopee PayLater?
  • Telat Bayar Listrik 1 Hari: Apa yang Terjadi?
  • Cara Mengunduh Foto Profil WhatsApp Teman di Android, iPhone, dan PC/Mac
  • Rekomendasi Aplikasi Edit Foto Ringan Terbaik untuk PC Windows dan macOS
  • Cara Membeli Diamond Mobile Legends Menggunakan Pulsa Telkomsel
  • Tutorial Menggunakan Aplikasi Dana: Cara Top Up Dana dengan Mudah, Cepat, dan Murah untuk Pemula
  • Website Konverter YouTube ke MP3 Terbaik 2025
  • Cara Mengatasi Otorisasi Kadaluarsa Higgs Domino Tanpa Login Facebook
  • Tips Main E-Football 2024: Strategi Pemilihan Tim dan Pemain Terbaik
  • DramaQ: Situs Nonton Drakor Sub Indo Terbaru dan Lengkap
  • IGLookup: Cara Download APK dan Informasi Lengkap
  • Cara Daftar DrakorID? Apakah DrakorID Streaming Penipu/Ilegal?
  • Cara Login, Register, dan Transfer Data MyKONAMI
  • Website PT Melia Sehat Sejahtera Apakah Penipuan?
  • Alternatif APK Bling2: Alternatif Stylish untuk Ekspresi Diri
  • Cara Menggunakan Stellarium Web
  • Cara Menghapus Data KTP Pribadi di Pinjol yang Belum Lunas
  • Cara Mengganti Nomor TikTok yang Tidak Aktif atau Hilang Tanpa Verifikasi

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