Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

PHP Cookbook: Reversing a String by Word or Byte

Posted on January 24, 2012

Problem
You want to reverse the words or the bytes in a string.

Solution
Use strrev( ) to reverse by byte, as in Example 1-19.

Example 1-19. Reversing a string by byte
[sourcecode language=”php”]<?php
print strrev(‘This is not a palindrome.’);
?>[/sourcecode]

Example 1-19 prints:
.emordnilap a ton si sihT
To reverse by words, explode the string by word boundary, reverse the words, and then rejoin, as in Example 1-20.

Example 1-20. Reversing a string by word
[sourcecode language=”php”]
<?php
$s = "Once upon a time there was a turtle.";
// break the string up into words
$words = explode(‘ ‘,$s);
// reverse the array of words
$words = array_reverse($words);
// rebuild the string
$s = implode(‘ ‘,$words);
print $s;
?>[/sourcecode]

Example 1-20 prints:
turtle. a was there time a upon Once

Discussion
Reversing a string by words can also be done all in one line with the code in Example 1-21.

Example 1-21. Concisely reversing a string by word
[sourcecode language=”php”]<?php
$reversed_s = implode(‘ ‘,array_reverse(explode(‘ ‘,$s)));
?>[/sourcecode]

See Also
Recipe 23.7 discusses the implications of using something other than a space character as your word boundary; documentation on strrev( ) at http://www.php.net/strrev and array_reverse( ) at http://www.php.net/array-reverse.

Sumber: Adam Trachtenberg and David Sklar, PHP Cookbook, O’Reilly.

Terbaru

  • Kenapa Aplikasi Bima Tri Gak Bisa Dibuka & Login? Ini Dia Penyebab & Solusinya!
  • Berapa Nilai Denda Telat Bayar Shopee Paylater?
  • Pengertian Paylater
  • Download Video Syakirah Viral Full Album: Lengkap & Mudah!
  • Apa itu USN IG (User Session Number Instagram)?
  • Transaksi Gagal Pakai SpayLater? Ini Dia Penyebab & Cara Mengatasinya!
  • Cara Bedain Bukti Transfer Asli & Palsu
  • Kaget Ada SMS Tagihan SPAYLATER! Ini Penjelasan Lengkapnya
  • Cara Mengatasi Error AD Domain Services Down
  • Akun SpayLater Kalian Nonaktif? Ini Dia Penyebab & Cara Mengatasinya!
  • Mengatasi Error: “Maaf, Layanan Spinjam Belum Tersedia untukmu”
  • Cara Mengatasi Komputer Restart Mendadak & Error Tak Terduga
  • Apa itu Apple Studio Display ?
  • Apakah AdGuard DNS Aman Buat Android Kalian?
  • Kenapa Kuota Nasional Tri Tidak Bisa Dipakai Padahal Masih Banyak?
  • Kenapa Saldo Rekening BNI Bisa Minus?
  • Apa itu Apple iPhone Lipat ‘Clamshell’?
  • ToonMe, Cara Ubah Foto Jadi Kartun Disney Gemoy
  • Apa itu Apple CarPlay Ultra?
  • Steam Wallet Pending Setelah Jual Barang? Ini Dia Penyebab & Cara Mengatasinya!
  • Apakah Lowongan PT Rifan Financindo Asli atau Modus Penipuan?
  • Waspada! Modus Digital Agency Palsu
  • Apakah BFI Finance Benar Investasi atau Penipuan?
  • YouTube TV Diupdate dengan Ikon Baru: Lebih Modern, Lebih Mudah!
  • Cara Ampuh Atasi ‘Service Under Construction’ Yandex: Jangan Panik!
  • Apple M5 Pro & M5 Max MacBook Pro: Kapan Rilisnya? Bocoran Terbaru!
  • Awas! Apakah APK Surya Rupiah Legal? MLM atau Penipuan?
  • iPhone Bea Cukai vs. Kemenperin, Mana yang Lebih Aman
  • Kenapa Video Cukur Kumis dan Rambut Viral di TikTok? Berawal dari Ngonten!
  • Review Aplikasi Barcode No Thanks
  • Linux News Roundup February 2026
  • How to Install JellyFin Media Server on Samsung TV with TizenOS
  • Why OneNote Clears Your Notes
  • AMD NPU Monitoring on Linux: A Beginner’s Guide to AI Chip Tracking!
  • How to Fix AMD Adrenalin’s Game Detection Issues on Windows
  • Cara Membuat Podcast dari PDF dengan NotebookLlama dan Groq
  • Tutorial Membuat Sistem Automatic Content Recognition (ACR) untuk Deteksi Logo
  • Apa itu Google Code Wiki?
  • Cara Membuat Agen AI Otomatis untuk Laporan ESG dengan Python dan LangChain
  • Cara Membuat Pipeline RAG dengan Framework AutoRAG
  • 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