PHP Common functions

Source: Internet
Author: User
Tags explode mathematical functions ord rtrim square root strtok

Mathematical functions

1.abs (): Absolute value
$abs = ABS (-4.2); 4.2 Numeric Absolute number

2.ceil (): Into a method to take the whole
echo ceil (9.999); 10 floating point number in one rounding

3.floor (): Rounding off the Rounding method
echo Floor (9.999); 9 floating-point numbers directly out of the decimal part

4.fmod (): floating-point number

  $x = 5.7;
  $y = 1.3;//two floating-point numbers, x>y floating-point remainder
  $r = Fmod ($x, $y);
 //$r equals 0.5, because 4 * 1.3 + 0.5 = 5.7

5.pow (): Returns the number of n-th square
 echo pow (-1, 20);//1 base number |n quadratic square value

6.round (): Floating-point rounding
 echo round (1.95583, 2);//1.96, a value | Preserves the number of digits after the decimal point, the default is 0 rounded result

7.sqrt (): square root
& Nbsp;echo sqrt (9); 3 square root of the radical

8.max (): Maximum value
Echo max (1, 3, 5, 6, 7);//7
Multiple numbers or arrays returns the maximum value
Echo max (Array (2, 4, 5));//5
9.min (): Minimum value
Input: Multiple numbers or arrays
output: Returns the minimum value

10.mt_rand (): Better random number
Input: Min | max, output: Random number value in Random return range
Echo Mt_rand (0,9);//n


11.rand (): Random number input: Min | max, output: Random number returns the value in the range

12.pi (): Get pi Value
To go to a space or other character:

13.trim (): Remove spaces or other predefined characters at each end of a string
$str = "\r\nhello world!\r\n";
echo Trim ($STR);
Input: Target string return value: Cleared string

14.rtrim (): Remove whitespace or other predefined characters to the right of the string
$str = "Hello world!\n\n";
echo RTrim ($STR);

15.chop (): Alias for RTrim ()

16.ltrim (): Remove space or other predefined characters to the left of the string
$str = "\r\nhello world!";
Echo LTrim ($STR);

17.dirname (): Returns the directory portion of the path
Echo dirname ("c:/testweb/home.php"); C:/testweb
Input: A string containing the path return value: Returns the directory portion of the file path

String generation and conversion:

18.str_pad (): Fills a string with the specified length
$STR = "Hello World";
Echo Str_pad ($str, 20, ".");
Input: string to fill | The length of the new string | The string to use for padding, which is blank by default
Output: String after completion

19.str_repeat (): repeating the specified string
Echo str_repeat (".", 13); String to repeat | The number of times a string will be repeated 13 points

20.str_split (): Splitting a string into an array
Print_r (Str_split ("Hello"));
Input: String to be split | The length of each array element, default 1
Output: Array of strings after splitting

21.strrev (): Invert string
Echo Strrev ("Hello world!"); !dlrow Olleh
Output: String after reverse order of target string

22.wordwrap (): String wrapping with a specified length
$STR = "An example in a long word is:
Supercalifragulistic ";
Echo WordWrap ($STR, 15);
Input: Target string | maximum width
Output: New string after wrapping

23.str_shuffle (): Randomly disrupts all characters in a string
Echo str_shuffle ("Hello World");
Input: Target string sequential output: scrambled string

24.PARSE_STR (): Parsing a string into a variable
Parse_str ("Id=23&name=john%20adams", $myArray);
Print_r ($myArray);
Input: String to parse | The array name of the stored variable
Output: return array ([id] = [name] = John Adams)

25.number_format (): Formatting numeric input by thousands grouping: number to format | Specifies how many decimals | Specifies a string to use as a decimal point | Specifies a string to use as a thousands separator
Output: 1,000,000 1,000,000.00 1.000.000,00

Case Conversion:

26.strtolower (): string converted to lowercase
Echo strtolower ("Hello world!");
Target String Lowercase string

27.strtoupper (): string converted to uppercase
Echo strtoupper ("Hello world!");
Output: Uppercase string

28.ucfirst (): Capitalize the first letter of the string
echo ucfirst ("Hello World"); Hello World


29.ucwords (): string each word first character to uppercase
echo ucwords ("Hello World"); Hello World
HTML Tag associations:

30.htmlentities (): Convert character to HTML entity
$str = "John & ' Adams '";
Echo htmlentities ($str, Ent_compat); John & ' Adams '

31.htmlspecialchars (): Predefined word representable HTML encoding

32.NL2BR (): \ n escaped as <br> label
Echo nl2br ("One line.\nanother line.");
Output: Processed string

33.strip_tags (): Stripping HTML, XML, and PHP tags
Echo strip_tags ("Hello <b>world!</b>");

 
34.addcslashes (): Adds a backslash to an escaped character in a string before the specified character
$str = "Hello, my name is John Adams.";
Echo $str;
Echo addcslashes ($str, ' m ');
Input: Target string | Specify a specific character or range of characters

35.stripcslashes (): Remove backslash added by addcslashes ()
Echo stripcslashes ("Hello, \my Na\me is Kai ji\m.");
Destination string Hello, my name is Kai Jim.


36.addslashes (): Add a backslash before specifying a predefined character
$str = "Who ' s John Adams?";
echo addslashes ($STR);
Output: "\" and null in the target string are escaped

37.stripslashes (): Remove the escape character added by Addslashes ()
Echo stripslashes ("who\ ' s John Adams?"); Clear escape sign who's John Adams?


38.quotemeta (): Add a backslash before some of the predefined characters in the string
$STR = "Hello World". (Can you hear me?) ";
echo Quotemeta ($STR);
Hello world\. \ (Can you hear me\?\)

39.CHR (): Returns the character from the specified ASCII value
Echo Chr (052); ASCII value returns the corresponding character


40.ord (): Returns the ASCII value of the first character of a string
echo ord ("Hello"); The ASCII value of the first character of a string
string comparison:

41.STRCASECMP (): Case-insensitive comparison of two strings
echo strcasecmp ("Hello world!", "Hello world!");
Input: Two target string output: Big 1| etc 0| Small-1

42.STRCMP (): Case-sensitive comparison of two strings

43.STRNCMP (): Compares the first n characters of a string, case sensitive
Call: int strncmp (String $str 1, String $str 2, int $len)

44.STRNCASECMP (): Compares the first n characters of a string, not case-sensitive
Call: int strncasecmp (String $str 1, String $str 2, int $len)

45.STRNATCMP (): Natural order Method compares string length, case-sensitive
Call: int strnatcmp (String $str 1, String $str 2)
Input: Target string

46.STRNATCASECMP (): Natural order Method compares string length, case-insensitive
Call: int strnatcasecmp (String $str 1, String $str 2)
String cutting and splicing:

47.chunk_split (): Dividing a string into small chunks
Call: Str chunk_split (str $body [, int $len [, str $end]]
Input: $body target string, $len length, $str insertion terminator output: Split string

48.strtok (): Cut string
Call: Str strtok (str $str, str $token)
Target string $str, $token for flag cut returns the cut string

49.explode (): Use one string to split another string for a flag
Call: Array explode (str $sep, str $str [, int $limit])
Input: $sep as a delimiter, $str the target string, $limit returns the array with a maximum number of elements output: The array formed after the string is split

50.implode (): joins, concatenate array values into strings with subscription characters
Call: String implode (String $glue, array $pieces)
$glue default, connect directly with "

51.SUBSTR (): Intercept string
Call: String substr (string $string, int $start [, int $length])


String Lookup substitution:
52.str_replace (): String substitution operation, case-sensitive
Call Mix Str_replace (Mix $search, Mix $replace, Mix $subject [, int & $num])
Input: $search Find the string, $replace the replacement string, $subject looked up string, & $num output: Returns the result of the substitution

53.str_ireplace () string substitution operation, case insensitive
Call: Mix Str_ireplace (Mix $search, Mix $replace, Mix $subject [, int & $count])
Input: $search find string, $replace replacement string, $subject,& $num output: Returns the replaced result

54.substr_count (): Counts the number of occurrences of a string in another string
Call: int Substr_count (String $haystack, string $needle [, int $offset = 0 [, int $length]])

55.substr_replace (): Replaces a string in another string
Call: Mixed Substr_replace (mixed $string, string $replacement, int $start [, int $length])

56.similar_text (): Returns the number of identical characters in two strings
Call: int Similar_text (str $str 1,str $str 2)
Input: Two strings for comparison
Output: Shaping, number of identical characters

57.STRRCHR (): Returns a string in another string where the last occurrence of the position begins at the end of the string
Call: String STRRCHR (String $haystack, mixed $needle)

58.STRSTR (): Returns a string in another string starting at the end of the string
Call: String Strstr (String $str, String $needle, bool $before _needle)

59.STRCHR (): An alias of Strstr () that returns the first occurrence of a string in another string, starting at the end of the string
Call: String Strstr (String $haystack, mixed $needle [, bool $before _needle = false])

60.STRISTR (): Returns a string in another string starting at the end of the string, case-insensitive

Call: String Stristr (String $haystack, mixed $needle [, bool $before _needle = false])

61.STRTR (): Converts some characters in a string

Call: String Strtr (String $str, String $from, String $to)

62.strpos (): Find where a character in a string first appears
Call: int Strpos (String $haystack, mixed $needle [, int $offset = 0])

63.stripos (): Looks for the first occurrence of a character in a string, case-insensitive invocation: int Stripos (String $haystack, string $needle [, int $offset])

64.strrpos (): Find the last occurrence of a character in a string
Call: int Strrpos (String $haystack, string $needle [, int $offset = 0])

65.strripos (): Finds the last occurrence of a character in a string, is not case sensitive
Call: int Strripos (String $haystack, string $needle [, int $offset])

66.STRSPN (): Returns the first mask-compliant substring length in a string called: int strspn (String $str 1, String $str 2 [, int $start [, int $length]])

67.STRCSPN (): Returns the length of a string in a string that does not match mask
Call: int strcspn (String $str 1, String $str 2 [, int $start [, int $length]])
Input: $str 1 is queried, $str 2 query string, $start the character to start querying, $length is the query length output: Returns from the beginning to the first few characters
String statistics:

68.str_word_count (): Statistics string contains the number of words
Call: Mix str_word_count (str $str, [])
Input: Target string output: Number of statistics offices

69.strlen (): Statistical string length int strlen (str $str)
Input: Target string output: integer length

70.count_chars (): Count all occurrences of letters in the string (0..255) Call: Mixed Count_chars (string $string [, int $mode])
String encoding:

71.md5 (): String MD5 encoding
$str = "Hello";
echo MD5 ($STR);


PHP Common functions

Related Article

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.