Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

PHP Cookbook: Controlling Case

Posted on February 18, 2012

Problem
You need to capitalize, lowercase, or otherwise modify the case of letters in a string. For example, you want to capitalize the initial letters of names but lowercase the rest.

Solution
Use ucfirst( ) or ucwords( ) to capitalize the first letter of one or more words, as shown in Example 1-25.

Example 1-25. Capitalizing letters
[sourcecode language=”php”]
<?php
print ucfirst("how do you do today?");
print ucwords("the prince of wales");
?>
[/sourcecode]

Example 1-25 prints:
How do you do today?
The Prince Of Wales

Use strtolower( ) or strtoupper( ) to modify the case of entire strings, as in Example 1-26.

Example 1-26. Changing case of strings
print strtoupper("i'm not yelling!");
// Tags must be lowercase to be XHTML compliant
print strtolower('one');

Example 1-26 prints:
I'M NOT YELLING!
one

Discussion
Use ucfirst( ) to capitalize the first character in a string:
[sourcecode language=”php”]
<?php
print ucfirst(‘monkey face’);
print ucfirst(‘1 monkey face’);
?>
[/sourcecode]

This prints:
Monkey face
1 monkey face

Note that the second phrase is not “1 Monkey face.”
Use ucwords( ) to capitalize the first character of each word in a string:

This prints:
1 Monkey Face
Don’t Play Zone Defense Against The Philadelphia 76-ers

As expected, ucwords( ) doesn’t capitalize the “t” in “don’t.” But it also doesn’t capitalize the “e” in “76-ers.” For ucwords( ), a word is any sequence of nonwhitespace characters that follows one or more whitespace characters. Since both ‘ and – aren’t whitespace characters, ucwords( ) doesn’t consider the “t” in “don’t” or the “e” in “76-ers” to be word-starting characters.

Both ucfirst( ) and ucwords( ) don’t change the case of non-first letters:

This prints:

MacWorld says I should get an iBook
ETunaFish.com Might Buy ItunaFish.Com!

The functions strtolower( ) and strtoupper( ) work on entire strings, not just individual characters. All alphabetic characters are changed to lowercase by strtolower( ) and strtoupper( ) changes all alphabetic characters to uppercase:

This prints:

i programmed the wopr and the trs-80.
“SINCE FEELING IS FIRST” IS A POEM BY E. E. CUMMINGS.

When determining upper- and lowercase, these functions respect your locale settings.

See Also
For more information about locale settings, see Chapter 19; documentation on ucfirst( ) at http://www.php.net/ucfirst, ucwords( ) at http://www.php.net/ucwords, strtolower( ) at http://www.php.net/strtolower, and strtoupper( ) at http://www.php.net/strtoupper.

Terbaru

  • Google Bikin Kejutan! Pixel 10 Diskon Gila-gilaan di YouTube Premium
  • Apa Itu Google CC? Ini Pengertian Agen Produktivitas AI Eksperimental Terbaru
  • Apa Itu Ultras Seblak di eSport? Pengertian dan Fenomena Baru Suporter eSport
  • Android 16: Animasi Folder Baru yang Mengubah Cara Kita Berinteraksi!
  • Android 16: Notifikasi Lokasi ‘Blue Dot’ – Fitur Baru yang Perlu Kalian Ketahui!
  • Apa Itu Risiko Auto Click di Event Spongebob Mobile Legends? Ini Penjelasannya
  • Apa Itu Fitur Eksperimental Windows? Ini Pengertian dan Cara Menonaktifkannya
  • Apa Itu Android 16 Beta 1? Ini Pengertian dan Fitur Terbarunya
  • Belum Tahu? Ini Trik Supaya Bisa Dapat Skin Patrick Mobile Legends dengan Harga Murah
  • Pixel Desember 2025: Update Besar Siap Meluncur, Apa yang Baru?
  • Apa Itu HYFE XL Prioritas? Ini Pengertian, FUP, dan Realita Kecepatannya
  • Pengertian Render dan Convert: Apa Bedanya dalam Video Editing?
  • Cara Mengatasi Aplikasi Office yang Terus Muncul dan Menerapkan Perubahan Pengaturan Privasi
  • Pixel Launcher Mendapatkan Sentuhan Google Search Baru!
  • Penyebab Aplikasi Wondr BNI Tidak Bisa Dibuka
  • Kode 0425 Daerah Mana? Ini Pengertian dan Fakta Sebenarnya
  • Apa Itu SSS CapCut? Pengertian Downloader Video Tanpa Watermark yang Wajib Kalian Tahu
  • Apa Itu Paket GamesMAX Telkomsel? Ini Pengertian dan Fungsinya Bagi Gamers
  • Apa Itu Menu Plus di Google Search? Ini Pengertian dan Fungsinya
  • Apa Itu Lepas Kolpri? Ini Pengertian dan Fenomenanya di Dunia Gaming
  • Pixel Buds Pro Dapat Update Software dengan Dukungan ANC Adaptif dan Peningkatan Audio
  • Mous Pixel Watch 4 Akan Hadir dengan Charger Baru dan Fitur-Fitur Terbaru
  • Hati-hati, Video Asli Botol Golda Viral Season 4 Full 6.30 Menit, Cek Link dan Faktanya disini!
  • Google Docs Dapat Update Material 3 dan Desain Ekspresif Baru, Fokus pada Kreativitas dan Kolaborasi
  • Belum Tahu? Ini Trik Hitung Cost Per Gigabyte Supaya Gak Boncos Saat Beli Paket Internet
  • Apple TV dan Google Cast Akan Terintegrasi Lebih Dalam, Tawarkan Pengalaman Streaming yang Lebih Baik
  • Lagi Rame Botol Golda Viral 19 Detik? APA ITU? Jangan Asal Klik Link Sembarangan, Ini Bahayanya!
  • Apa itu Regedit FF Auto Headshot 100 No Password 2024? Simak Definisi dan Cara Kerjanya
  • Apa Itu KPH dan MPH? Pengertian dan Perbedaannya di Game Balap
  • Belum Tahu? Inilah Cara Aman Quotex Login dan Solusi Kalau Error!
  • Apa Itu Platform Modular Intel Alder Lake N (N100)? Ini Pengertian dan Spesifikasinya
  • Apa Itu Armbian Imager? Pengertian Utilitas Flashing Resmi untuk Perangkat ARM Kalian
  • Apa Itu OpenShot 3.4? Pengertian dan Fitur LUT Terbaru untuk Grading Warna
  • Flatpak 1.16.2: Sandbox Baru untuk GPU Intel Xe dan VA-API
  • Apa Itu EmmaUbuntu Debian 6? Pengertian Distro Ringan Berbasis Trixie untuk PC Lawas
  • Apa Itu Nemotron-3 Nano? Pengertian Model Bahasa Ringkas dan Hasil Uji Cobanya
  • Prompt AI Dapur Aestetik
  • Prompt AI Suami Istri Bawa Terong
  • Prompt AI Touring Motor di Stadion GBK
  • Prompt AI Foto Jadul Kebaya Merantau Belanda
  • Apa Itu Insiden Peretasan Kementerian Dalam Negeri Prancis? Kronologi dan Dampaknya
  • Apa Itu DPRK Threat Actors? Ini Pengertian dan Sepak Terjangnya dalam Pencurian Kripto Global
  • Apa itu Insiden Siber DXS International? Pengertian dan Dampaknya pada NHS
  • Apa Itu Warrantless Search Google? Pengertian dan Dampaknya Bagi Privasi Kita
  • Kronologi Kasus Malware Kapal Feri Fantastic
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

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