Summary of PHP Common String Functions of Recommendation

  • 2021-10-25 06:12:34
  • OfStack

1. Function to determine type


is_bool()    // Determine whether it is Boolean 
is_float()   // Determine whether it is floating point type 
is_real()    // Ibid. 
is_int()    // Determine whether it is an integer 
is_integer()  // Ibid. 
is_string()   // Determine whether it is a string 
is_object()   // Judge whether it is an object or not 
is_array()   // Determine whether it is an array 
is_null()    // Determine whether it is null
is_file()    // Determine whether it is a file 
is_dir()    // Determine whether it is a directory 
is_numeric()  // Determine whether it is a number 
is_nan()    // Judgment is not a number 
is_resource()  // Determine whether it is a resource type 
is_a($obj,$classname) // Determine whether an object is an instance of a class 
           // Available  if($obj instanceof Classname)

2. Get the substring position


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  

3. Get a substring


substr($str,$start [,$length]); // Get a substring 
substr_compare($main_str,$str,$offset[,$length]); // Substring comparison   From offset Start comparison at 
substr_count($hs,$nd [,$offset=0 [,$length]]); // Get a substring nd In hs Number of occurrences in 
substr_replace($string,$replacement,$start [,$length]); // String substring replacement 
                // Use $replacement Replace $string From start Start length is length Substring of 
strstr($hys,$nd [,bool $before_needle = false ]);// Return $nd In $hys  No. 1 1 The beginning of the second occurrence to the end of the string   String until 
        // No. 1 3 If the parameter is true  Returns the $nd  Previous string 
stristr($hys,$nd [,bool $before_needle = false ]); // Same as above, ignoring the case version 
strrchr($hys,$nd); // Return $nd In $hys Finally 1 The beginning of the second occurrence to the end of the string   String until 
        //1 Equal harmony  substr(strrchr($hys,$nd),strlen($nd));  Sharing 
strpbrk($haystack,$char_list);// From $char_list Matches individual characters in turn in the $haystack Middle grade 1 The place where it appeared for the first time 
            // To the end of the string   The entire string of 
strtok($str,$token); // No. 1 1 Secondary use   String by separator $token Segmentation 
strtok($token);   // No. 1 2 Secondary use 
	eg.
	$string = "This is\tan example\nstring";
	/*  Use tabs and line breaks as delimiters  */
	$tok = strtok($string, " \n\t");
 
	while ($tok !== false) {
		echo "Word=$tok<br />";
		$tok = strtok(" \n\t");
	}

4. String str_ function


str_getcsv($str); // Will csv File string is converted to 1 Array of numbers 
str_replace($search,$replace,$subject [,&$count]);// Search and replace strings 
      // No. 1 4 If a parameter is specified, it will be assigned the number of times it will be replaced 
str_ireplace($search,$replace,$subject [,&$count]);// Search and replace strings 
      // No. 1 4 If a parameter is specified, it will be assigned the number of times it will be replaced   Ignore case 
str_shuffle(string $str);// Randomly scramble a string 
str_split($str [,$len=1]);// Converts a string to a 1 Array of numbers 
             // The length of each array cell is $len

5. String length

strlen ($str); //String length

6. Flip the string

strrev (string $string); //Flip string

7. mb_ Type String Function

mb_ type string is basically the same as the above string function,
Just add one more optional character encoding parameter, which is the same as above
Here are 1 other useful functions

1. Detect the character encoding of the string


$encode = mb_detect_encoding($lines, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
if($encode != "UTF-8"){
  $lines = iconv($encode,"UTF-8", $lines);
}

8. String-related operations

1. Convert string types


strval($str); // Convert to string type 
floatval($str);// Convert to floating point type 
intval($str); // Convert to integer type 

2. Case conversion


strtolower($str); // Convert all to lowercase 
strtoupper($str); // Convert all to uppercase 

3. String to timestamp


strtotime($str); // Convert a string in time format to an integer timestamp 
      // Pay attention to setting the time zone   Otherwise there will be  8 Hourly error 

4. Remove HTML and PHP labels


strip_tags($str [,$tags]);// Remove without $tags All labels outside the inside label 

5. ascii to digital to ascii


chr(int $ascii); // Digital conversion to ascii
ord(string $str); // Return $str No. 1 1 Character of ascii Value 

6. Encoding and decoding of json


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
0

7. Wrap lines and turn them < br/ >


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
1

8. Array to string, string to array


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
2

9. Thousand-bit partition formatting


string number_format ( float $number [, int $decimals = 0 ] )
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
 @param  $number  The number you want to format  
     $decimals  Decimal places to keep  
     $dec_point  Specifies the characters displayed by the decimal point  
     $thousands_sep  Specifies the characters displayed by the thousand separator  

10. Remove the blanks


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
4

This function deletes the white space character at the end of str and returns.

Without the second parameter, rtrim () removes only the following characters:
"" (ASCII 32 (0x20)), common white space.
"\ t" (ASCII 9 (0x09)), tabs.
"\ n" (ASCII 10 (0x0A)), line break.
"\ r" (ASCII 13 (0x0D)), carriage return.
• "\ 0" (ASCII 0 (0x00)), NUL null byte character.
"\ x0B" (ASCII 11 (0x0B), vertical tabs.
The filter character can also be specified by the charlist parameter. 1 to list all the characters you want to filter,
You can also use ".." to list a 1-character range

11. Transform string encoding function


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
5

12. String encryption function


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
6

13. String escape and inversion function


strpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the.  
stripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  The position of the number that first appears in the ,  Case-insensitive. 
strrpos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 The position of the number that occurs next time. 
strripos($hs,$nd [,int $offset = 0 ]) // Return nd  In  hs  Middle last 1 Number position of next occurrence , Case-insensitive.  
7

14. Return data in format


sprintf  -   Return the data as required, but do not output it 
	 The representable types are as follows: 
	string s 
	integer d, u, c, o, x, X, b 
	double g, G, e, E, f, F 
	eg.
	$num = 5;
	$location = 'tree';
	$format = 'There are %d monkeys in the %s';
	echo sprintf($format, $num, $location);

Related articles: