|
strrev
Reverse a string
(PHP 4, PHP 5)
Example 2463. Reversing a string with strrev()<?php Code Examples / Notes » strrevcarmel.alex
to lwc at mytrashmail dot com, take it easy. function utf8_strrev($str, $reverse_numbers = true){ $pattern = $reverse_numbers ? '/./us' : '/(\d+)?./us'; preg_match_all($pattern, $str, $ar); return join('',array_reverse($ar[0])); } screend
this function can only reverse the 1-byte words,like english,it seems,using <?php $str=strrev("ºº×Ö"); echo $str; ?> do not get a right result. but,you can change 2-bytes characters into a ASCLL,the converse it. avarab
strrev() can be very useful in cases where it makes more sense to do something from the end of a string rather than the beginning (well duh!) such as apply certain regular expressions. Here's a small function to add commas to numbers that works in such a way. <?php echo commafy("1500000.1254"); // prints 1,500,000.1254 function commafy($_) { return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) ); } ?> I originally wrote it in Perl, does it show? ;=) lwc
just as well for UTF-8 usages = I meant also for NONE UTF-8 usages (to keep the numbers unchanged)
tex
Just a correction to the previous commenter. In ISO 8859-15, the Euro is 0xA4 (164 decimal). It is a 1 byte character.
magicaltux
I will make Screend at hf dot webex dot com's comment more clear and understandable. strrev only works for singlebyte character-sets. Multibytes charactersets are not compatibles with strrev. US-ASCII and ISO-8859-1 are compatible with strrev, however BIG5, SJIS, UTF-8 aren't. Despite what you can think, ISO-8859-15 *is* multibyte (the euro symbol - - is coded on two bytes). There's no mb_strrev function in PHP, so you can't strrev() a multibyte string. Try to convert it to something else with iconv() if it can be represented in a singlebyte character set. lwc
/* Here's a function that adds to carmel.alex's utf-8 encoding support the ability NOT to reverse numbers (for example when you output a phrase as a parameter for a SWF file that can't handle RTL languages itself, but obviously any numbers should remain the same as in the original phrase). Note that it can be used just as well for UTF-8 usages if you want the numbers to remain intact: */ function utf8_strrev($str, $reverse_numbers) { preg_match_all('/./us', $str, $ar); if ($reverse_numbers) return join('',array_reverse($ar[0])); else { $temp = array(); foreach ($ar[0] as $value) { if (is_numeric($value) && !empty($temp[0]) && is_numeric($temp[0])) { foreach ($temp as $key => $value2) { if (is_numeric($value2)) $pos = ($key + 1); else break; } $temp2 = array_splice($temp, $pos); $temp = array_merge($temp, array($value), $temp2); } else array_unshift($temp, $value); } return implode('', $temp); } } // "It says this site is copyrighted just from 2001" (in Hebrew) $str = "×ת×× ×©××תר ××× ×××× ××××××ת ××צר×× ×¨×§ ××× 2001"; // Reverse everything $str_blind_reverse = utf8_strrev($str, true); // Reverse everything but don't change the year 2001 to 1002... $str_logical_reverse = utf8_strrev($str, false); |
Change Languageaddcslashes addslashes bin2hex chop chr chunk_split convert_cyr_string convert_uudecode convert_uuencode count_chars crc32 crypt echo explode fprintf get_html_translation_table hebrev hebrevc html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode quotemeta rtrim setlocale sha1_file sha1 similar_text soundex sprintf sscanf str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split str_word_count strcasecmp strchr strcmp strcoll strcspn strip_tags stripcslashes stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk strpos strrchr strrev strripos strrpos strspn strstr strtok strtolower strtoupper strtr substr_compare substr_count substr_replace substr trim ucfirst ucwords vfprintf vprintf vsprintf wordwrap |