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

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center