PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )

Image Source

Repository

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

What Will I Learn?

I will learn the String Methods ( Addslaches, Chr, Chop and Chunk_splite ) concepts and examples.

  • How to protect SQL requests using Addsalches method.
  • What's the Chr method and how to use it.
  • How to control the string by deleting a specific part using Chop method.
  • What's the Chunk_splite method and its uses.

Requirements

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

Difficulty

  • Basic

Description

1 - Addslaches Method 

 The function returns a text string preceded by backslashes before the characters to be changed. These characters are 

  • Single quotation marks (') 
  •   Double quotation marks (") 
  •  Backslashes (\) 
  • NUL 

Some uses the addslashes () function to prevent "injecting SQL code" (SQL Injection). Instead, the functions of database strings should be used. It has the text string as parameter.  

To use the addslaches method you need to pass the " String " as parameter

addslaches($string);

I will use the addslaches method to protect the SQL request from SQL Injection and this the code

$sql = "Select * from Students where idStudent = 1 or ' 1 = 1 ' ";

echo addslashes($sql);

The method will add backslashes before the single quotation and this is the result

2 - Chr Method 

 The chr () function returns a specified character corresponding to ASCII encoding. This function complements its ord() counterpart. It has the code ASCII as parameter .

To use the Chr method you need to pass the ASCII code

chr(ascii code);

I will pass an ASCII code to the Chr method to get the letter, you can learn about ASCII code from here

echo chr("100");

The representation of ASCII code 100 is the letter " d " and this is the result

3 - Chop Method 

 The chop() function will remove a white space or other predefined character from the right end of a string. It has two parameters The String and the part that you want to remove.

To use the Chop method you must pass " the String and the word " as parameters

chop($string, "word");

The method will remove from the right end and it will remove the word you passed as parameter

$string = "I love PHP";

echo chop($string, "PHP");

The string is " I love PHP " I want to remove the word PHP and this is the result

4 - Chunk_Splite Method 

The chunk_split () function can be used to divide a text string into small blocks. 

It has 3 parameters  The first parameter is The body which is The text string to be divided. The second is The length of the block. The third is the End which is the text string that will be inserted in the end. 

The chunk_split () function returns the fragmentary text string.  

To use the chunk_splite we pass 3 parameters 

chunk_splite($string, length, chunklen);

The chunk splite method will devide a string by length using the divider 

$stars = "********************";

echo chunk_split($stars, 4 , " | ");

I have a string of stars I want to divide every 4 stars by the separator " | " and this is the result

Video Tutorial 

Curriculum

Proof of Work Done

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

Sort:  

Hi @alexendre-maxim

Thank you for your contribution to the video tutorial category.

I can see that you have made improvement from the suggestions given to you in the past. The fonts that are in your video are much bigger and easier to follow.

When you do your tutorial, do you have a cheat sheet with you or do you start speaking as you turn on the microphone and the computer?

I often outline everything that I want to speak in the video tutorial so that it is easy for students to follow from one step to the next in a clear format. I notice at times you have pauses. Something like this will help you become more professional in your presentation.

I look forward to see your next video.

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? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

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

Yes, I will try to write more notes to help me make the best , thank you @rosatravels

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

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!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 56478.46
ETH 2384.59
USDT 1.00
SBD 2.34