In PHP tutorial tutorial this time I will discuss a simple function that is quite often used, namely substr () function. The substr () function is used to bypass a string or to extract some value from a string inside PHP.
Getting to know the substr () function
The function substr () is a PHP function to bypass a string, or to extract some value from a string. This feature is quite often used in the process of creating PHP programs, especially those that require string manipulation.
For example, suppose we have a date-shaped string: "26-06-2016". How to retrieve the month value of the string, the "06" character?
Another example, say a student's NIM consists of 10 digits: 1557301058 ". The first two digits are the year of student entry, the next two digits of the department code, and the last four digits are the serial number of the student. How do I separate these digits?
and the following below I will explain one by one how to use this subtr () function.
<?php
$meuruno = "Belajar PHP Notepad Bek ka eh";
$sub_meuruno = substr($meuruno,0);
echo $sub_meuruno;
// meuruno PHP bek ehh
?>
<?php
$meuruno = "You And I ";
$sub_meuruno = substr($meuruno,1);
echo $sub_meuruno;
// How to Make Character from Early String
?>
<?php
$meuruno = "You And I ";
$sub_meuruno = substr($meuruno,4,-2);
echo $sub_meuruno;
// How to Make Character from middle String
?>
Output
explanation
The function substr () also has arguments (4, -2) which are optional (may be filled or emptied). If we add a fourth argument, this value serves as a determinant of 'how many characters to extract'
Last, How to Take Characters From the last String?
Apart from the start of the string, we can also take characters starting from the end of the string. The trick is to assign a negative value to the second argument of the substr () function. Immediately we see examples of its use:
<?php
$meuruno = "You And I ";
$sub_meuruno = substr($meuruno,7);
echo $sub_meuruno;
// How to Make Character from last String
?>
Output
explanation
Partial retrieval of string values is quite often used. PHP provides substr () functions for this purpose. the substr () function is very practical for cutting a string or taking some value from a string inside PHP.
This is my first contributions