preface
Collected several common functions in file upload and utilization.
An in-depth understanding of these functions should contribute to the smooth upload and utilization of files.
Indexes
1. deldot
2. in_array
3. intval
4. strrchr
5. strtolower
6. strrpos
7. str_ireplace
8. strstr
9. substr
10. trim
Common functions
1. deldot
deldot function is a common function in upload lab. It is actually a user-defined function defined in common In PHP, the functions are defined as follows:
function deldot($s){ for($i = strlen($s)-1;$i>0;$i--){ $c = substr($s,$i,1); if($i == strlen($s)-1 and $c != '.'){ return $s; } if($c != '.'){ return substr($s,0,$i+1); } } }
That is, start from the end of the string and delete the point from the back to the front, Until the end of the string, the character is not until.
Therefore, for the following inputs,
echo deldot("hello world")."\n"; echo deldot("hello world.")."\n"; echo deldot("hello world....")."\n"; echo deldot("hello.world.")."\n";
Output as
hello world
hello world
hello world
hello.world
2. in_array
in_array(mixed $needle, array $haystack, bool $strict = false): bool
The first parameter $need is the value to be searched, $haystack is the array to be searched, and the third parameter determines whether to conduct type comparison.
The third type defaults to false, that is, whether the types are the same or not is not considered.
For the following inputs:
if(in_array("AAA",$arr,false)) echo 1; if(in_array("aaa",$arr,false)) echo 2; if(in_array("AAA",$arr,true)) echo 3; if(in_array("aaa",$arr,true)) echo 4;
output
13
3. intval
intval(mixed $value, int $base = 10): int
The intval function gets the integer value of the variable.
The first parameter $value is the variable to get the integer value, which can be string, value and array.
The second parameter, $base, specifies the base used for the conversion and is valid if and only if the variable to be converted is a string.
When the second parameter is 0, the format of the variable is detected to determine the conversion base used.
- Hexadecimal is used when there is a leading 0x or 0x.
- When there is a leading 0, octal is used.
- Otherwise, use decimal.
The value returned by the intval function is a value of type int. 0 is returned when the conversion is unsuccessful.
In particular, note that there is an upper limit on the value returned using this function. When the converted value is greater than the integer range of php, the returned result is the upper limit of the integer value.
echo intval("111"); echo "\n"; echo intval("111a"); echo "\n"; echo intval("0x333"); echo "\n"; echo intval("888",8); echo "\n"; echo intval("122",3); echo "\n"; echo intval("11111111111111111111111111111111111"); echo "\n"; echo intval("2222222222222222222222222222222");
Output as
111
111
0
0
17
9223372036854775807
9223372036854775807
4. strrchr
strrchr(string $haystack, mixed $needle): string
The strrchr function looks for $need in the string $haystack and returns the last found $need and its subsequent strings. If $need is not found in the string, false is returned.
Note:
- If the second parameter is not a single character, only the first character of the string is used for lookup matching.
- If the second parameter is a value, the value is converted to the corresponding ASCII code for matching.
$S = "hhhahahaha2333"; echo strrchr($S,'h')."\n"; echo strrchr($S,'hwweraer')."\n"; echo strrchr($S,104)."\n"; if(strrchr($S,'k') == false) echo "false";
ha2333
ha2333
ha2333
false
5. strtolower
strtolower(string $string): string
Converts each English character in the string $string to lowercase and returns it.
$S = "HaHaHaHa,Hello!!"; echo strtolower($S);
hahahaha,hello!!
6. strrpos
strrpos(string $haystack, string $needle, int $offset = 0): int
Returns the last occurrence of the character $need.
In php4, $need can only be a single character. If there are multiple characters in $need, only the first character is used for matching.
Similar to strrchr, if $need is a numeric value, the ASCII character corresponding to the numeric value is used for matching.
Starting with php5, $need can be multiple characters.
Starting from php5, strrpos adds a parameter $offset, which can specify where to start matching from $haystack.
Returns the matched subscript position. If there is no match, false is returned.
be careful:
- Because the return value may be zero, the all equal sign = = = = must be used when judging whether the return value is false.
- This function is case sensitive. Functions similar to this function are:
- stripos: find the location of the first occurrence, case insensitive.
- strpos: find the location of the first occurrence, case sensitive.
- strripos: find the last occurrence, case insensitive.
- That is, if "i" appears, it is case insensitive, and if "rr" appears, it is the last one to find.
$s = "Phpphphpphpp"; echo strrpos($s,"php"); echo strrpos($s,"h"); echo strrpos($s,"P"); if(strrpos($s,"PHP") === false) echo "No exist";
Output:
890No exist
Note: the PHP version used in the test is 5.3.3.
The results may be different in PHP4.
7. str_ireplace
str_ireplace( mixed $search, mixed $replace, mixed $subject, int &$count = ? ): mixed
str_ The ireplace function is used to replace elements in an array or substrings in a string.
The first parameter $search is the content to be replaced (substring or array), the second parameter $replace is the content to be replaced (string or array), and the third parameter $subject is the replaced string.
- If both $search and $replace are strings, the matching substring $search in $subject will be replaced with $replace.
- If both $search and $replace are arrays, mapping replacement will be performed. If the number of values of $replace is less than the number of search, the redundant replacement will be performed with an empty string.
- If $search is an array and $replace is a string, $replace will be used to replace every search element in $subject.
$count can be used to limit the number of replacements.
Note:
- The replacement takes place from left to right.
- This function substitution is case insensitive. (another function str_replace is case sensitive)
<?php echo str_ireplace("php","","hello.php")."\n"; echo str_ireplace("pHP","","hello.Php")."\n"; echo str_ireplace("php","","hello.phPHpp")."\n"; echo str_ireplace("php","p","hello.phphp")."\n"; echo str_ireplace("php",""."phpphpphpphpphp.php",3)."\n"; echo str_ireplace(array("php","html"),"","hello.php.html")."\n"; echo str_ireplace(array("php","html"),array("1","2"),"hello.php.html")."\n"; echo str_ireplace(array("php","html","css"),array("1","2"),"hello.php.css.html")."\n"; echo str_ireplace(array("php","html","css"),"1","hello.php.css.html")."\n"; foreach (str_ireplace(array("php","html","css"),array("1","2","3"),array("hello.html","hello.css","hello.html")) as $it){ echo "$it "; } ?>
hello.
hello.
hello.Hpp
hello.php
3
hello...
hello.1.2
hello.1...2
hello.1.1.1
hello.2 hello.3 hello.2
8. strstr
strstr(string $haystack, mixed $needle, bool $before_needle = false): string
Find the position where the string $need first appears in $haystack and return $need and subsequent strings.
Add the third parameter $before from PHP5_ Need, if $before_ If the value of need is true, the part in front of $need is returned.
$s = "123phpphp.php"; echo strstr($s,"php")."\n"; echo strstr($s,"php",true)."\n";
phpphp.php
123
9. substr
substr(string $string, int $offset, ?int $length = null): string
Returns a substring in the string $string.
$offset specifies the subscript position of the first character of the substring in $string, and $length specifies the length of the truncated substring.
Value of $length:
- When $length is the default value, the function will intercept and return the string from $offset to $length.
- When $length is positive, it will intercept and return up to $length characters from $offset.
- When $length takes 0, an empty string is returned.
- When $length takes a negative number, it will return $offset to the character before the penultimate $length of string $string.
$s = "123456789"; echo substr($s,1,3)."\n"; echo substr($s,1,-1)."\n"; echo substr($s,1)."\n"; echo substr($s,1,0)."\n";
234
2345678
23456789
10. trim
trim(string $str, string $character_mask = " \t\n\r\0\x0B"): string
Remove whitespace from the beginning and end of the string $str.
When the second parameter remains the default, the characters to be removed are:
- "" space
- "\ t" tab
- "\ n" line break
- "\ r" carriage return
- "\ 0" null byte character
- "\ x0B" vertical tab
$s = "\n 1 23456789\t\n123456789\r"; echo trim($s);
1 23456789
123456789
reference material
Note: This article is only for technical sharing and should not be used for illegal purposes. Otherwise, the author will not be responsible for the consequences.
Original is not easy, thank you for your support.