PHP Flip String

Source: Internet
Author: User

//Method OnefunctionStrrev_charset ($string,$charset= ' Utf-8 '){    if(!is_string($string) || !mb_check_encoding ($string,$charset)){        return' Flip failed '; }Else{        $arr= []; $str _len= Mb_strlen ($string,$charset);  for($i= 0;$i<$str _len;$i++){            $char= Mb_substr ($string,$i, 1,$charset); Array_unshift($arr,$char); }        return implode($arr); }}//Method TwofunctionUtf8_strrev ($str){    //u This modifier opens an additional feature that is incompatible with Perl. The pattern string is considered utf-8. This modifier is available from the UNIX version of PHP 4.1.0 or higher, and the Win32 version of PHP 4.2.3. PHP 4.3.5 Start checking the utf-8 legitimacy of the pattern. s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].     Preg_match_all('/./us ',$str,$array);//Put each character in the array    return Join(‘‘,Array_reverse($array[0]));//flipping an array, converting to a string}$str= ' World Hello 123 fight hard ' abc ';EchoStrrev_charset ($str);//CBA fight 321 good you worldtobr ();EchoUtf8_strrev ($str);//CBA fight 321 good you worldtobr ();Echo Strrev($str);//contain Chinese when garbled, CBA?? 漋 Ningyi? 321?? 堽? jaundice?


conclusion:
method a function used:
is_string (string) to determine whether it is a string;
mb_check_encoding (string,encoding) to check if the string is valid in the specified encoding;
mb_strlen (string,encoding), gets the length of the string (can contain Chinese);
mb_substr (string,start,lenth,encoding) to get a partial string (which can contain Chinese);
array_unshift (array,new _value,new_value2,...), inserts a new element at the beginning of the array,
implode (array), which returns a string composed of array elements. It is not recommended to omit the first argument, the way the second argument is placed in the first position, and the
implode (Separator,array), which returns a string composed of array elements. It is recommended to use this.


Method two uses the function:
Preg_match_all (Pattern,string,match_result), performs a global regular expression match (note that the matched result array is a two-dimensional array);
Array_reverse (Array,preserve), returns the array in the reverse element order (whether the second parameter retains the key name);
Join (), which is the alias of the Implode () function.

PHP Flip String

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.