https://github.com/php/php-src
I will learn the string methods this is the sixth part , we will take the " Quotemeta, Rtrim, Sha1_File and Sha1 " methods.
The quotemeta function adds backslashes (a backslash \) in front of a few predefined characters in a string.
It returns the string after inserting a backslash in front of all the following characters.
The predefined characters are
Point (.)
backslash (\)
plus (+)
Multiplication sign (*)
interrogation point (?)
Accolade []
sign of omission (^)
dollar sign ($)
parenthesis ()
It has the string as parameter and it returns a string.
To use the quotemeta method you must pass the string as parameter
quotemeta($string)
I want to use an example with almost all the predefined characters to see the effect of the quotemeta method
echo "<h3>Quotemeta Method</h3>";
$string = "20 + 20 = 40$
20 * (20 - 10 ) = ?
20 - [20 - 10] = ?
20^20 = ?
";
echo quotemeta(nl2br($string));
I have used ' nl2br ' method to save the form of the string , I have explained this method in the previous tutorial , this is the result
The rtrim function returns the string after removing all end-of-string white characters.
If it called without the second parameter rtrim will remove the following characters:
- "" (ASCII 32 (0x20)) an ordinary space.
- "\ t" (ASCII 9 (0x09)), a tab.
- "\ n" (ASCII 10 (0x0A)) a new line (line feed).
- "\ r" (ASCII 13 (0x0D)) a carriage return.
- "\ 0" (ASCII 0 (0x00)) the character NUL.
- "\ x0B" (ASCII 11 (0x0B)) a vertical tab.
It has two parameters
The first is the string to verify.
The second is the charlist .
To use the rtrim method you need to pass the string and the charlist as parameters
rtrim($string, charlist)
I have a string " I love Utopian " and I want to apply the rtrim method to this string to remove the work Utopian
$string2 = "I love Utopian";
echo rtrim($string2, "Utopian");
The method will remove from the right the characters passed as parameter and this is the result
The sha1_file function calculates the SHA-1 hash of a file, calculate the sha1 of the file specified by the filename parameter using US Secure Hash Algorithm 1, then return that sha1.
The sha1 is a hexadecimal number of 40 characters. it returns a string of characters on success, FALSE otherwise.
It has two parameters
The first is the file to calculate.
The second is a Boolean parameter , specifies the spell or binary result format:
TRUE - the binary format of 20 characters .
FALSE -By default, number of 40 characters .
To use the sha1_file you must pass the file name as parameter
sha1_file($file_name)
The file "Utopian.txt" contains the string " I love Utopian ! " I want to get the SHA-1 hash of the file using this method
$sha1_file = sha1_file("utopian.txt");
echo $sha1_file;
The method by default with the value false , it will return 40 characters and this is the result
The sha1 function calculates the SHA-1 hash of a string, the function sha1 computes the sha1 of a strings using US Secure Hash Algorithm 1, it returns a string of characters on success, FALSE otherwise.
It has two parameters
The first is the string to calculate.
The second is a the raw_output parameter , If the optional raw_output parameter is changed to TRUE, the sha1 is returned in raw binary form with a size of 20 characters, otherwise it is returned as a hexadecimal number with a size of 40 characters.
To use the sha1 method you need to pass the string as parameter
sha1($string)
I want to get the SHA-1 hash of a string which is the same of the file " I love Utopian !"
$string3 = "I love Utopian !";
$sha1 = sha1($string3);
echo $sha1;
Logically it will return the same value that the previous method returned with 40 characters, this is the result
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/string6.php