Share on Google+Share on Google+

PHP get character from String

Advertisement
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

 

Advertisements

Advertisement

Posted on: October 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.