Home > Backend Development > PHP Tutorial > PHP string flip example code_PHP tutorial

PHP string flip example code_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 17:45:48
Original
1130 people have browsed it

Character flipping is a piece of cake for PHP to process. PHP's string function strrev can handle it. For example:

echo strrev("Hello World!"); //The output result is "!dlroW olleH"

But sometimes during interviews we often need to write a function ourselves to achieve the same effect as strrev. In fact, this is not difficult, for example:

/**

* Function to implement string flipping

* @param string $str The string to be processed

* @return string The successfully flipped string

​*/

function reverse($str){

If($str == ''){

        return null;

}

If(strlen($str) == 1){

         return $str;

}else{

          $string = "";

for($i=1;$i<=strlen($str);$i++){

                $string .=substr($str,-$i,1);

}

          return $string;

}

}

echo reverse("Hello World!"); //The output result is "!dlroW olleH"

Excerpted from: Shine’s Holy Paradise

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478625.htmlTechArticle Character flipping is a piece of cake for PHP processing. PHP’s string function strrev can handle it. For example : echo strrev(Hello World!); //The output result is !dlroW olleH But sometimes interviews...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template