PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )

Image Source

Repository

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

What Will I Learn?

I will learn the string methods this is the third part , we will take the " Implode, Join, Lcfirst, Ltrim and Fprintf " methods.

  • How to convert from array to string using " Implode " method.
  • What's the " Join " method and its uses.
  • How to transform the first letter of a string to lower case by " Lcfirst " method.
  • How to remove special caracters from the left of a string using " Ltrim " method.
  • What's the " Fprintf " Method and how to format the string. 

Requirements

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

Difficulty

  • Basic

Description

1 - Implode Method :  

The implode function assembles the array elements to form a text string using the glue parameter. 

It accepts parameters in any order they were However, in order to approve the explode method we need to follow this order of parameters 

- The first parameter is the Glue :  By default, it's specified a blank text string. 

- The second parameter is the Pieces : The data array from which we form the text string. 

The implode function returns a text string that contains all the elements of the array and the same order within the array, with the addition of the glue parameter between the elements.  

To use the implode method we need to pass two parameters 

implode(glue, pieces);

I have an array of string and I want to form a string using the elements of this array

$array = array("PHP", "is", "the", "best", "!");

$str = implode(" ", $array);

echo $str;

The implode method will return the elements of the array with the space between them as a string and this is the result

2 - Join Method :  

The join function returns a string of array elements. It has two parameters :  

The first is the separator : By default, it is worth the empty string . This is not the recommended use of the join method. We recommend that you always use both settings to be compatible with older versions. 

The second is the array :  Which is The array of chains to collect.  

To use the join method we need to pass two parameters

join(separator , $array );

The same example , I have an array and I will form a string using the elements of the array and the separator

$array2 = array("www", "utopian", "io");

$str2 = join(".", $array2);

echo $str2;

The separator is the point " . " , the join method will return a link " www.utopian.io " and this is the result

3 -  Lcfirst Method :  

The lcfirst method returns a string whose first character was lowercased, if that character is an alphabetic character. The 'alphabetical' is determined by the current locale.Currently, characters like a-umlaut (ä) will not be converted. 

It has just the String as parameter and it returns a string.  

To use the lcfirst method we need to pass the string as parameter

lcfirst($string);

I have a string and I want to change the form of the first letter without CSS proprieties using the lcfirst method

echo lcfirst($str);

The lcfirst return the string , we can directly print it using " echo " method

4 -   Ltrim Method :  

The ltrim method will remove spaces or other predefined characters from the beginning of the string. This function returns the string, after removing the invisible characters from the beginning of the string. 

If the second charlist parameter has been omitted, ltrim 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 parameter the string and the charlist and it returns a string.  

To use the ltrim method we need to pass the string

ltrim($string);

I have a string with spaces at the beggining , I want to remove them using the ltrim method

5 -   Fprintf Method :  

The fprintf method writes a text string formatted with the format parameter to a path defined by the handle. It has two parameters : 

The first is The handle :  From file system resources, which is typically created via the fopen function. 

The second is the format : We have many options :  

  • % : Percentage sign. No media required. 
  • %b: will be treated as an integer and presented as a binary number. 
  • %c: will be treated as an integer and presented as a character with the corresponding ASCII value. 
  • %d: will be treated as an integer and rendered as a decimal (with a reference). 
  • %f: will be treated as a decimal and displayed as a decimal point number  
  • %g: shortcut to use% e and% f. o: will be treated as a valid number and presented as an octal number. 
  • %s: will be treated as a text string. 
  • %u: will be treated as an integer and rendered as a decimal (non-location). 
  • %x: The argument will be treated as an integer and presented as hexadecimal (in lower case). 
  • %X: The argument will be treated as an integer and presented as hexadecimal (in capital letters).

To use the fprintf method we need to pass two parameters

fprintf($file, format);

I want to write a number with many format in new file " utopian.txt "

$num = 22;

$file = fopen("utopian.txt", "w");

echo fprintf($file, "%d", $num);

The fprintf method return the length of characters in the file , I have the number = 22 and depending on the "% " the format will be changed

Video Tutorial 

Curriculum

Proof of Work Done 

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

Sort:  

Hello @alexendre-maxim

I can see more improvement in this video tutorial as you try to be more structured in your presentation and also picking up some teaching techniques here to make your lessons easier to follow.

You will notice from the review score that your overall quality have increased.

All I can say is to keep at it and stay focused on presenting the concept in an organized manner.

Happy New Year 2019. Wishing you much success in this coming year.

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!

Thank you @rosatravels , Happy new year ^_^

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.21
TRX 0.20
JST 0.033
BTC 97309.89
ETH 3288.22
USDT 1.00
SBD 2.99