PHP get character from String


 

PHP get character from String

PHP get character from String tutorial shows you how to get substring from a string

PHP get character from String tutorial shows you how to get substring from a string

PHP Get Character From String

To get sub string from a String we can use substr() method. Structure of this method is :

string substr (string $var, int $initial [, int $len]  )

In the above example we want to get a sub string of variable $var, the initial index position is denoted by $initial and the length is given by $len, which is optional. If we don't mention the length PHP consider it as last value.

Code:

<?php

$string = "Goodbye, Varun!";

$a = substr($string, 0);

$b = substr($string, 1);

$c = substr($string, 2);

$d = substr($string, 50);

$e = substr($string, 5, 4);

$f = substr($string, 10, 1);

echo $a."<br/>";

echo $b."<br/>";

echo $c."<br/>";

echo $d."<br/>";

echo $e."<br/>";

echo $f."<br/>";

?>

Output:

Goodbye, Varun!
oodbye, Varun!
odbye, Varun!

ye,
a

 

Ads