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

  • 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