PHP Realizing the Function Example of String Conversion Function from Case to Case

  • 2021-11-24 01:00:51
  • OfStack

The case conversion function of string is often used in daily life. So how to implement a simple case conversion function?

In php, the toupper of c language is finally used, and the tolower function converts characters to case. Therefore, you need to define a function for 1-character case conversion.


// Character to uppercase protected function toupper($c){
$ord = ord($c);
return $ord>=97 && $ord<=122 ?chr($ord-32):$c;}// Character to lowercase protected function tolower($c){
$ord = ord($c);
return $ord>=65 && $ord<=90 ?chr($ord+32):$c;}

Character case conversion is the conversion of ascii code. The ASCII code of A-Z is between 65 and 90. The ASCII code of a-z is between 97 and 122. Characters that are not in the conversion interval should be returned as they are

The string case conversion in php has the following functions: strtolower, strtoupper, lcfirst, ucfirst, ucwords, lcfirst. These functions are all paired, so we only take uppercase to lowercase as an example to illustrate how to implement these functions

strtoupper implements string conversion from uppercase to lowercase. It is nothing more than traversing every character of the string and converting uppercase characters to lowercase.


public function strtolower($str){
 if($this->checkempty($str))
 {
 return "";
 }
 $len = strlen($str);
 for($i=0;$i<$len;$i++){
 $str[$i] = $this->tolower($str[$i]);
 }
 return $str;}

php strings can get each character with a subscript like Array 1. Therefore, every character of the string can be traversed and converted to lowercase characters

The lcfirst is simpler than the strtolower because it enables capitalization of the first letter


public function ucfirst($str){
 if($this->checkempty($str))
 {
 return "";
 }
 $str[0] = $this->toupper($str[0]);
 return $str;}

lcwords converts the first letter of a word to lowercase. When you say a word, it is actually the first character after the space. Therefore, only the first non-empty string after traversing to the space character needs to be converted to lowercase.


public function lcwords($str){
 if($this->checkempty($str))
 {
 return "";
 }
 $splitchar = [' ',"\n","\r","\f","\v"];
 $len = strlen($str);
 for($i=0;$i<$len;$i++){
 if(in_array($str[$i], $splitchar))
 {
 $i++;
 if($i>=$len)
 {
 break;
 }
 $str[$i] = $this->tolower($str[$i]);
 }
 }
 return $str;}

Mainly be careful of cross-border problems. If the last 1 string is a null character.

As for why the word segmentation character is the code of those several items, mainly php source code is based on those several items. The implementation of ucwords in php source code is as follows:


PHP_FUNCTION(ucwords){
 zend_string *str;
 char *delims = " \t\r\n\f\v";
 register char *r, *r_end;
 size_t delims_len = 6;
 char mask[256];

 ZEND_PARSE_PARAMETERS_START(1, 2)
 Z_PARAM_STR(str)
 Z_PARAM_OPTIONAL Z_PARAM_STRING(delims, delims_len)
 ZEND_PARSE_PARAMETERS_END();

 if (!ZSTR_LEN(str)) {
 RETURN_EMPTY_STRING();
 }
 php_charmask((unsigned char *)delims, delims_len, mask);

 ZVAL_STRINGL(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
 r = Z_STRVAL_P(return_value);

 *r = toupper((unsigned char) *r);
 for (r_end = r + Z_STRLEN_P(return_value) - 1; r < r_end; ) {
 if (mask[(unsigned char)*r++]) {
 *r = toupper((unsigned char) *r);
 }
 }}

The divided string is put into one mask, and whether it is the character of mask is judged during traversing the string. If yes, the next 1-bit character is converted to uppercase.

Final code address https://github.com/froyot/froyot.github.io/tree/master/code

Summarize


Related articles: