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

  • Ini Arti Kode Error FP26EV dan FP27EV di Aplikasi BriMO Serta Cara Mengatasinya!
  • Cara Jadi Clipper Modal Ngedit Video di TryBuzzer, Cocok Banget Buat Pemula!
  • Cara Pakai Beb6 Wifi Password Untuk Cek Jaringan di Sekitar Kalian
  • Inilah Review Lengkap Apakah Turbo VPN Extension Aman
  • Apa itu Toko Biru? Ini Istilah Olshop yang Perlu Kamu Tahu
  • Adswerve Inc Penipu? Ini Fakta di Balik Konsultan Digital Terkemuka Dunia!
  • Sering Ditelepon 08111? Simak Apakah Ini Penipuan atau Marketing Resmi Telkomsel
  • Inilah Cara Daftar dan Login Subsidi Tepat LPG 3 kg di Merchant Apps MyPertamina!
  • Akademi Crypto Penipu?
  • Belum Tahu Arti Paket Sedang Transit di TikTok Shop? Ternyata Ini Maksudnya Biar Kalian Nggak Panik!
  • Akun Paylater Tiba-tiba Diblokir? Jangan Panik, Gini Cara Mengatasinya Biar Lancar Jaya!
  • HP Vivo Sering Mati Sendiri Padahal Baterai Masih Banyak? Ini Penyebab dan Solusinya!
  • Inilah Cara Mengatasi BNI Mobile Banking Transaksi Tidak Dapat Dilanjutkan dan Penjelasan Kode Errornya!
  • Inilah Daftar Lengkap Tempat OVO PayLater Bisa Digunakan
  • Gini Caranya Biar Nggak Mati Topik Pas Lagi Main RP Role Play
  • Gini Caranya Atasi Kontak yang Sudah Dihapus Tapi Masih Ada di WA, Dijamin Ampuh!
  • Cara Mengatasi DJP Online Error 403 atau 405 dengan Mudah
  • Sering Lihat Kata Puqi di TikTok? Jangan Asal Tulis, Ternyata Ini Artinya!
  • Apa Itu HP Refurbished?
  • Cara Backup WhatsappGB Tanpa Hilangkan Chat
  • Belum Tahu? Ini Trik Dapat Battle Emote Ejen Ali Mobile Legends Gratis Permanen!
  • Kelebihan dan Kekurangan Paypal Indonesia 2026
  • Inilah Fungsi Web Browser Terbaru yang Bikin Internetan Kalian Jauh Lebih Aman!
  • Ini Cara Pinjaman Online Bank Mandiri Langsung Cair via Livin Tanpa Ribet
  • Gagal Tukar Pengguna Dapodik? Jangan Panik, Ini Trik Ampuh Buat Jenjang PAUD dan SD!
  • Game Terasa Berat Padahal HP Bagus? Ini Trik Setting WebView yang Jarang Orang Tahu!
  • Cara Simpan Video FreeReels ke Galeri Tanpa Watermark, Dijamin Aman dan Resmi!
  • Capek Mancing Terus? Coba Trik AFK Fish It Roblox 2026 Ini, 100% Gratis Tanpa Langganan!
  • Profil Andy Dahananto, Pilot Pesawat ATR Indonesia Air Yang Jatuh di Maros, Sulawesi Selatan
  • Update Pencarian Pesawat IAT: Suara Ledakan di Maros Bikin Geger, Ini Fakta Terbarunya!
  • Grain DataLoader Python Library Explained for Beginners
  • Controlling Ansible with AI: The New MCP Server Explained for Beginners
  • Is Your Headset Safe? The Scary Truth Bluetooth Vulnerability WhisperPair
  • Dockhand Explained, Manage Docker Containers for Beginners
  • Claude Co-Work Explained: How AI Can Control Your Computer to Finish Tasks
  • Tutorial Langfuse: Pantau & Optimasi Aplikasi LLM
  • Begini Teknik KV Caching dan Hemat Memori GPU saat Menjalankan LLM
  • Apa itu State Space Models (SSM) dalam AI?
  • Begini Cara Mencegah Output Agen AI Melenceng Menggunakan Task Guardrails di CrewAI
  • Tutorial AI Lengkap Strategi Indexing RAG
  • Apa itu Skandal BlackCat Ransomware?
  • Apa itu ToneShell? Backdoor atau Malware Biasa?
  • Apa itu Parrot OS 7? Ini Review dan Update Terbesarnya
  • Clipper Malware? Ini Pengertian dan Bahaya yang Mengintai Kalian
  • Kronologi Serangan Gentlemen Ransomware di Oltenia Energy
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