PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )

in utopian-io •  6 years ago 

Image Source

Repository

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

What Will I Learn?

I will learn the string methods the second part we will take the " Bin2Hex, Count_Chars, Explode and Hex2Bin " methods

  • How to convert from Binary to Hexadecimal using the Bin2Hex method.
  • What's the Count_Chars method and how to use it .
  • How to divide the string using the Explode method.
  • How to convert from Hexadecimal to Binary using the Hex2Bin method.

Requirements

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

Difficulty

  • Basic

Description

1 – Bin2Hex Method

The bin2hex function converts the binary representation data to hexadecimal representation. The bin2hex function returns a text string encoding ASCII that contains hexadecimal representation of the string parameter. 

It has " The String " as parameter and it returns the hexadecimal representation of the entered text string. 

To use the Bin2Hex method you need to pass the string as parameter

bin2hex($string)

In this example I will convert a string " PHP is the best ! " to the Hexadecimal representation using " Bin2Hex " method

$str = "PHP is the best !";    

$b2x = bin2hex($str);       

echo "PHP is the best ! in HEX : " . $b2x; 

I created a variable " b2x " to contain the returning value from the method and I passed the string as parameter , this is the result

2 – Count_Chars Method

The count_chars function returns information about the characters in the text string. The function returns the number of iterations for each byte-value from the value 0 to 255 in a text string in different ways.

It has the string as parameter and it returns 4 results depending on the number that you give as parameter. 

To use the count_chars method you must pass the string and the num as parameters

count_chars($string, num)

The method returns 4 results depending to the num passed as parameter , for example if the " num " equals to 1 it will return an array with ASCII code of values exist in the string passed as parameter, also if the " num " equals to 3 it will return a string with all the letters used in the string .

3 – Explode Method

The explode function divides a text string into sections and returns it as an array. The explode function returns an array of text strings. Each text string is a segmentation of the total text string by dividing it at the delimiter string. 

It has 3 parameters : 

  • The first is the delimiter : The string selector, which is the type in which the text string will be divided. 
  • The second is string The entered text string to segment. 
  • The third parameter is limit : If the parameter value is positive, the returned array will contain elements with a limit number, with the last element in the array containing the rest of the text string. If the parameter value is negative, the returned array will contain all text string characters except another -limit element.  

To use the explode method you pass the delimiter and the string as parameters

explode(delimiter,$string)

I will use the same string " PHP is the best ! " as parameter to the explode method , it will return an indexed array starts from 0 to the number of the last element.

 print_r(explode(" ", $str)); 

The delimiter is the space , the explode method will cut the element before the space and add it to the array , finally it will return the indexed array

4 – Hex2Bin Method 

The hex2bin function decodes the hexadecimal encoding of the binary representation strings. The hex2bin function does not convert the hexadecimal representation of numbers to binary representation, but uses the base_convert () function to perform such a transformation. 

It has " the data " as parameter and it returns the binary representation of the entered data, or the FALSE value in case of failure.

The hex2bin function causes the error E_WARNING if the length of the text string is odd or has a hexadecimal representation.  

To use the Hex2Bin method you must pass the string as parameter

hex2bin($string)

I will use the " b2x " variable that I have created in the previous example to convert it from the hexadecimal representation to the binary

echo hex2bin($b2x); 

The function will return a binary data from the hexadecimal representation and this is the result

Video Tutorial 

 

Curriculum

Proof of Work Done 

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi @alexendre-maxim

Thank you for your contribution to the video tutorial.

I am very pleased with the improvement you have made in the tutorial. The presentation is getting better and also you have made the 'fonts' to good sizes for learners to follow easily.

I also see you varying your tones in your lecturing speech. This is important because learners can pick up the emphasis and follow your lessons easier.

I am sure as you continue on to develop better outlines and structures, your tutorials will continually improve.

I can see that you have taken the suggestions given to you. Thank you for implementing them.

You will be happy to see the higher score this time. Merry Christmas.

Have a happy holiday!

Rosa

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!

@rosatravels Thank you very much and I am really happy that I provide content that helps the users and followers to understand this language.

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

Thank you

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!

Thank you