How to Split or Retrieve String using substr() php function

in #utopian-io6 years ago (edited)

What Will I Learn?

  • You will learn How to Take Character From Early String
  • You will learn How to Take Character From Middle String
  • You will learn How to Take Characters From the last String

Requirements

  • You should be able to program basic php and HTML
  • You have to have text editor, browser, and web server to run it, then in this tutorial I use text editor that is (Notepad ++), and using chrome browser, and its web server is XAMPP Control Pane v3.2.2.

Difficulty

  • Basic

Tutorial Contents

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.

  • first turn on XAMPP as its web server
    1.jpg
  • then open notepad as text editor, then input program script like in picture below
    2.jpg
  • then open the chrome browser to display the results of the notepad coding that has been typed in the example;
<?php
$meuruno = "Belajar PHP Notepad Bek ka eh";
$sub_meuruno = substr($meuruno,0);
echo $sub_meuruno;
// meuruno PHP bek ehh
?>
  • then the results on its browser are as below;
    3.jpg
  • Now ,How to Take Character From Early String ?
    The first use of the substr () function that we will discuss is how to take a character that starts from the beginning of the string. Here is an example of its use:
<?php
$meuruno = "You And I ";
$sub_meuruno = substr($meuruno,1);
echo $sub_meuruno;
// How to Make Character from Early String 
?>
  • Output
    4.jpg
  • explanation
    In the code above, I took the string $ meuruno starting from the index to-1. If the substr () function is written with 2 arguments like this, and the second argument is positive, then the substr () function returns the string value $ meuruno starting from the 2nd letter, the letter "O" to the end of the string.
  • Then, How to Take Character From Middle String ?
    Here is an example of its use:
<?php
$meuruno = "You And I ";
$sub_meuruno = substr($meuruno,4,-2);
echo $sub_meuruno;
// How to Make Character from middle String 
?>
  • Output
    5.jpg

  • 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
    6.jpg

  • 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.

Curriculum

This is my first contributions



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules.

Contributions need to be informative and descriptive in order to help readers and developers understand them.

End-user focused tutorials must address a minimum of three substantial concepts that are unique to the Open Source project and essential learning requirements for end-users.

Just using the substr() function isn't complex enough to write a tutorial about. In the future I would recommend finding something to write about that isn't as straightforward.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 64403.69
ETH 3463.55
USDT 1.00
SBD 2.50