Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Makalah
  • Ke-NU-an
  • Kabar
  • 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

  • Cara Memperbaiki Error 0x8000FFFF Catastrophic Failure Saat Ekstrak Zip
  • Cara Memperbaiki File Explorer Crash Saat Membuka Folder Besar di Windows 11/10
  • Cara Mengatasi Error Login 0x8007003B di Outlook, Microsoft, XBox dll
  • Cara Memulihkan Akun Admin Microsoft 365 Karena MFA Gagal
  • Cara Mengatasi Error “A Conexant audio device could not be found”
  • Cara Memperbaiki Windows Tidak Nyala Lagi Setelah Sleep/Locked
  • Cara Memperbaiki Komputer Crash karena Discord
  • Cara Memperbaiki Error Windows “Failed to update the system registry”
  • Cara Memperaiki LGPO/exe/g
  • Cara Memperbaiki Error Tidak bisa Add Calendar di Outlook
  • Cara Memperbaiki File Transfer Drop ke 0 di Windows 11
  • Cara Memperbaiki Microsoft Copilot Error di Outlook
  • Cara Memperbaiki Error Virtualbox NtCreateFile(\Device\VBoxDrvStub) failed, Not signed with the build certificate
  • Cara Memperbaiki Error “the system detected an address conflict for an IP address, with Event ID 4199”
  • Cara Memperbaiki Password Microsoft Edge yang Hilang
  • Cara Memperbaiki Email Outlook yang Hilang atau Tidak Muncul
  • Cara Menemukan Username dan Password di Windows 11
  • Cara Mengatasi Error Virtualbox not detecting Graphics Card di Windows 11
  • Cara Mengatasi Error Windows MFReadWrite.dll not found or missing
  • Cara Membuat Formulir Menggunakan Zoho Form
  • Pemerintah Ganti Ujian Kesetaraan Dengan TKA 2025
  • Ini Perbedaan TKA vs Ujian Nasional: TKA Lebih Sakti?
  • Daftar TKA Tutup 5 Oktober: Sudah 3.3 Juta Yang Daftar
  • Review Aplikasi ClipClaps: Penipuan atau Tidak?
  • Review Aplikasi Wibuku: Alternatif Nonton Anime Gratis untuk Para Wibu Indonesia!
  • Inilah Alat dan Software Phone Farming dengan Samsung Galaxy J7 Prime
  • Cara Cek Paket Internet Telkomsel Kena Pembatasan/Throttling Atau Tidak
  • Cara Mengatasi YMusic APK Error Tidak Bisa Dibuka
  • Cara Memblokir Akun Teman di Mobile Legend: Panduan Lengkap
  • Profil Farida Farichah, Wakil Menteri Koperasi Kabinet Merah Putih Reshuffle 17 September 2025
  • Cara Memperbaiki Error 0x8000FFFF Catastrophic Failure Saat Ekstrak Zip
  • Cara Memperbaiki File Explorer Crash Saat Membuka Folder Besar di Windows 11/10
  • Cara Mengatasi Error Login 0x8007003B di Outlook, Microsoft, XBox dll

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