Get sub from the string start in PHP

Description

The following code shows how to get sub from the string start.

Example


//from  www  .j  a v a  2s .co  m
<?php
    echo substr("Hello world",6);
    //Using the start parameter with different positive and negative numbers:

   echo substr("Hello world",10)."<br>";
   echo substr("Hello world",1)."<br>";
   echo substr("Hello world",3)."<br>";
   echo substr("Hello world",7)."<br>";

   echo substr("Hello world",-1)."<br>";
   echo substr("Hello world",-10)."<br>";
   echo substr("Hello world",-8)."<br>";
   echo substr("Hello world",-4)."<br>";


   //Using the start and length parameters with different positive and negative numbers:

   echo substr("Hello world",0,10)."<br>";
   echo substr("Hello world",1,8)."<br>";
   echo substr("Hello world",0,5)."<br>";
   echo substr("Hello world",6,6)."<br>";

   echo substr("Hello world",0,-1)."<br>";
   echo substr("Hello world",-10,-2)."<br>";
   echo substr("Hello world",0,-6)."<br>";
   echo substr("Hello world",-2-3)."<br>";
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone