PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )

Image Source

Repository

https://github.com/php/php-src 

What Will I Learn?

I will learn the string methods this is the sixth part , we will take the " Quotemeta, Rtrim, Sha1_File and Sha1 " methods.   

  • What's the quotemeta method and how to use it.
  • How to delete from the right using Rtrim method.
  • The concept of Sha1_file method and its uses.
  • How to calculate the SHA-1 hash of a string using SHA1 method.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example) 

Difficulty

  • Basic

Description

 1- QuoteMeta method 

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

2- Rtrim method 

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

3- Sha1_File Method

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

4- Sha1 Method

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

Video Tutorial 

Curriculum

Proof of Work Done 

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/string6.php

Sort:  

Hi @alexendre-maxim

Thank you for your contribution to the video tutorial.

Your presentation is getting more clear.

Since these concepts are all basic learning about the strings and the variation methods, I am wondering whether there are more in depth tutorials to take students deeper.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!