strrev

(PHP 4, PHP 5)

strrevПереворачивает строку задом наперед

Описание

string strrev ( string $string )

Возвращает строку string, перевернутую задом наперед.

Список параметров

string

Переворачиваемая строка.

Возвращаемые значения

Возвращает перевернутую строку.

Примеры

Пример #1 Пример использования strrev()

<?php
echo strrev("Hello world!"); // выводит "!dlrow olleH"
?>

Коментарии

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.
2004-03-30 23:17:46
http://php5.kiev.ua/manual/ru/function.strrev.html
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? ;=)
2005-05-07 03:53:17
http://php5.kiev.ua/manual/ru/function.strrev.html
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.
2005-05-12 09:20:23
http://php5.kiev.ua/manual/ru/function.strrev.html
Just a correction to the previous commenter. In ISO 8859-15, the Euro is 0xA4 (164 decimal). It is a 1 byte character.
2005-08-23 00:36:11
http://php5.kiev.ua/manual/ru/function.strrev.html
This function support utf-8 encoding

function utf8_strrev($str){
    preg_match_all('/./us', $str, $ar);
    return join('',array_reverse($ar[0]));
}
2006-02-28 02:54:32
http://php5.kiev.ua/manual/ru/function.strrev.html
/*
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);
2006-10-13 16:35:13
http://php5.kiev.ua/manual/ru/function.strrev.html
just as well for UTF-8 usages = I meant also for NONE UTF-8 usages (to keep the numbers unchanged)
2006-10-13 19:01:39
http://php5.kiev.ua/manual/ru/function.strrev.html
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]));
}
2006-12-21 00:38:48
http://php5.kiev.ua/manual/ru/function.strrev.html
Автор:
MOD10, Modulus10 or also called LUHN10 will generate a valid check digit. 

<?php
$inv 
"34586";
echo 
$inv checkdigit($inv);

// Outputs 345868

function checkdigit($num) {
$sum 0;
$pos 0;
$rev strrev($num);
$len strlen($num);
if (
$len == 0$len += 1;
while (
$pos $len) {
$odd $rev[$pos] * 2;
if (
$odd 9) {
$odd -= 9;
}
$sum += $odd;
if (
$pos != ($len 2)) {
$sum += $rev[$pos +1];
}
$pos += 2;
}
return ((
floor($sum/10) + 1) * 10 $sum) % 10;
}
?>
2007-12-18 16:30:31
http://php5.kiev.ua/manual/ru/function.strrev.html
here is my version for strings with utf8-characters represented as numerical entities  (e.g. &#1234;)

function utf8_entities_strrev($str, $preserve_numbers = true) 
{
  //split string into string-portions (1 byte characters, numerical entitiesor numbers)

  $parts=Array();
  while ($str)
  {
    if ($preserve_numbers && preg_match('/^([0-9]+)(.*)$/',$str,$m))
    {
      // number-flow
      $parts[]=$m[1];
      $str=$m[2];
    }
    elseif (preg_match('/^(\&#[0-9]+;)(.*)$/',$str,$m))
    {
      // numerical entity
      $parts[]=$m[1];
      $str=$m[2];
    }
    else
    {
      $parts[]=substr($str,0,1);
      $str=substr($str,1);
    } 
  }

  $str=implode(array_reverse($parts),"");

  return $str;
}
2008-05-27 07:35:07
http://php5.kiev.ua/manual/ru/function.strrev.html
manfred at werkzeugH dot at, your version works, but do you think it has an advantage over mine? Just wondering if I should update my scripts.

carmel.alex at gmail.com, your version fails with utf8_strrev($string, true). It's something about spaces.

For example, you turn
This software was protected by 2000 patents since 2001
into
2001 ecnis stnetap2000 yb detcetorp saw erawtfos sihT

(note the "stnetap2000").
2008-06-19 08:00:02
http://php5.kiev.ua/manual/ru/function.strrev.html
An easy way for unicode string reversing;

<?php
function strrev_utf8($str) {
    return 
join(""array_reverse(
       
preg_split("//u"$str)
    ));
}

$str "Şeker yârim!"// My sugar love
echo $str."\n";
echo 
strrev($str)."\n"// !mir��y reke��
echo strrev_utf8($str); // !mirây rekeŞ
?>
2012-02-24 18:34:29
http://php5.kiev.ua/manual/ru/function.strrev.html
This is funny function for write upside down:
<?php

function strflip($text)
{
 
$text=strrev($text);
 
$text=strtolower($text);

$arr = array(
"a" => "&#x250;",
"b" => "q",
"c" => "&#x254;",
"d" => "p",
"e" => "&#x01DD;",
"f" => "&#x25F;",
"g" => "&#x387;",
"h" => "&#x0265;",
"i" => "&#x0131;",
"j" => "&#x027E;",
"k" => "&#x029E;",
"l" => "&#x0283;",
"m" => "&#x026F;",
"n" => "u",
"p" => "d",
"q" => "b",
"r" => "&#x0279;",
"t" => "&#x0287;",
"u" => "n",
"v" => "&#x028C;",
"w" => "&#x028D;",
"y" => "&#x028E;",
"B" => "D"
);
$result strtr($text,$arr);
return 
$result;
}
$text "My name is Dyas Yaskur"
echo 
strflip($text); //ɹnʞsɐʎ sɐʎp sı ǝɯɐu ʎɯ
?>
2012-03-28 02:09:11
http://php5.kiev.ua/manual/ru/function.strrev.html

    Поддержать сайт на родительском проекте КГБ