PHP implementation of String flipping method _php Tutorial

Source: Internet
Author: User

How to implement string flipping in PHP


This article mainly introduces PHP implementation of string flipping method, involving PHP operation string skills, with a certain reference value, the need for friends can refer to the next

The example in this paper describes how PHP implements string flipping. Share to everyone for your reference. The implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Header ("Content-type:text/html;charset=utf-8");

/**

The function is to reverse the Chinese string

Mb_strlen () Gets the length of the character

Mb_substr () gets a single element of the character

Krsort () sorts an array in reverse order of key values

Implode () stitching an array into a string

Explode () separating strings with strings

*/

function Str_rev_gb ($STR) {

Determine if the input is not a UTF8 type character, otherwise exit

if (!is_string ($STR) | |! Mb_check_encoding ($str, ' UTF-8 ')) {

Exit ("The input type is not a string of type UTF8");

}

$array =array ();

Depositing strings into an array

$l =mb_strlen ($str, ' UTF-8 ');

for ($i =0; $i < $l; $i + +) {

$array []=mb_substr ($str, $i, 1, ' UTF-8 ');

}

Invert string

Krsort ($array);

Stitching strings

$string =implode ($array);

return $string;

}

$str 1 = "englist";

$str 2 = "中文版 China";

$str 3 = "Eng lish country";

$str 4 = "People's Republic of China";

echo $str 1. " ". STR_REV_GB ($str 1)."
";

echo $str 2. " ". STR_REV_GB ($str 2)."
";

echo $str 3. " ". STR_REV_GB ($str 3)."
";

Echo $str 4. " ". STR_REV_GB ($str 4)."
";

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/975120.html www.bkjia.com true http://www.bkjia.com/PHPjc/975120.html techarticle the method of implementing string flipping in PHP this article mainly introduces the PHP implementation of string flipping methods, involving PHP operation string skills, with a certain reference value, the need for friends can participate ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.