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

  • Turning TikTok into a Money-Making Machine
  • Cara Ekstrak Driver Intel RST/VMD dari setuprst.exe: Panduan Lengkap
  • Pixel 8 dan 8 Pro Akan Punya Kamera Baru Resolusi 10.2MP, Hasil Lebih Baik di Kondisi Redup
  • Inilah Trik Website Kalian Lolos Core Web Vitals dan Ranking Naik
  • YouTube TV Uji Coba Fitur Tonton Rekaman Pertandingan Olahraga NFL,NBA, MLB Terbatas
  • Aawi Wireless Dua Habis Stok, Model Android Auto Tunggal Masih Diskon
  • Samsung Akan Luncurkan One UI 8.5 dengan Inspirasi ‘Liquid Glass’ yang Memukau
  • XBox Game Pass PC Tidak Bisa Address GPU ke Game
  • Your Pocket-Sized Doctors: 3 Health Apps Changing the Game on Android and iOS
  • Waymo Bawa Teknologi ‘Liquid Glass’ untuk Mobil Otonom
  • Rumor Google Akan Update UI Besar-besaran Desember 2025
  • Gemini Akan Masuk di Android Auto, Mobil Jadi Lebih Smart!
  • OpenAI Bantah Rencana Pasang Iklan di ChatGPT Berlangganan
  • Kenapa Komputer Sangat Panas Saat Gunakan Fitur Virtualisasi Hyper-V?
  • Apa itu Bug React2Shell? Sudah Serang Lebih dari 30 Organisasi dan 77.000 IP Address
  • Google Store Black Friday 2025: Penawaran Spesial untuk Pixel, Nest, dan Lainnya!
  • Boxville 2 Gratis di Playstore, Plus Diskon Lainnya!
  • Cara Atasi Masalah Pembacaan Suara (Read Aloud) di Windows Copilot Tidak Berfungsi
  • Kementerian Kesehatan Inggris Akui Data Breach, Akibat Zero-day Oracle DB?
  • Google Akan Perkenalkan Autofill Google Wallet di Chrome untuk Pembayaran Lebih Mudah
  • Google Pixel Akan Perkenalkan Launcher Device Search Baru, Lebih Cepat dan Pintar
  • Hacker Serang Bug VPN di ArrayOS AG untuk Menanam Web Shell
  • Cara Menonaktifkan Error “ITS Almost time to restart in Windows”
  • Google Fi Mendukung Panggilan Telepon RCS Melalui Web, Lebih Mudah dan Efisien
  • Data Breach Marquis: Hajar Lebih Dari 74 Bank dan Koperasi AS
  • Google Search Akan Adopsi ‘Continuous Circle’ untuk Hasil Pencarian Terjemahan, Lebih Cerdas dan Kontekstual
  • Rusia Memblokir Roblox Karena Distribusi ‘Propaganda LGBT’
  • Google Gemini Redesain Web Total di Desember 2025, Fokus UX yang Lebih Baik
  • Apa itu Google Workspace Studio? Tool Baru untuk Pembuat Konten?
  • Cara Menggunakan Xbox Full-Screen Experience di Windows
  • Turning TikTok into a Money-Making Machine
  • Cara Ekstrak Driver Intel RST/VMD dari setuprst.exe: Panduan Lengkap
  • Pixel 8 dan 8 Pro Akan Punya Kamera Baru Resolusi 10.2MP, Hasil Lebih Baik di Kondisi Redup

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