PHP 5.6.0beta1 released

strrev

(PHP 4, PHP 5)

strrevInvierte una string

Descripción

string strrev ( string $string )

devuelve la cadena, invertida.

Parámetros

cadena

La cadena que será invertida

Valores devueltos

devuelve la cadena invertida.

Ejemplos

Ejemplo #1 Invertir una cadena con strrev()

<?php
echo strrev("Hello world!"); // outputs "!dlrow olleH"
?>

add a note add a note

User Contributed Notes 4 notes

up
-1
dthawkins+php at gmail dot com
7 days ago
Reverse a string without using any functions

function reverse_string($s) {
        $i = 0;
        $rev = '';
        while ( $s[$i] ) {
                $i++;
        }
        $i--;
        while ( $s[$i] ) {
                $rev .= $s[$i];
                $i--;
        }
        return $rev;
}
up
-2
carmel.alex at gmail.com
8 years ago
This function support utf-8 encoding

function utf8_strrev($str){
    preg_match_all('/./us', $str, $ar);
    return join('',array_reverse($ar[0]));
}
up
-3
manfred at werkzeugH dot at
5 years ago
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;
}
up
-4
MagicalTux at FF dot st
8 years ago
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-* are compatible with strrev, however BIG5, SJIS, UTF-8 aren't.

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.
To Top