https://github.com/php/php-src
I will learn the string methods this is the third part , we will take the " Implode, Join, Lcfirst, Ltrim and Fprintf " methods.
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
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
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
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:
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
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 :
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
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/str3.php