Home Tutorial Php Examples PHP get character from String

 
 

PHP get character from String
Posted on: October 9, 2009 at 12:00 AM
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

 

Related Tags for PHP get character from String:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.