- Forums
- PHP
- How To Get The First 2 Characters In A String With Php Substr
This Page Contains information about How To Get The First 2 Characters In A String With Php Substr By wallpaperama in category PHP with 4 Replies. [1235], Last Updated: Sat May 18, 2024
wallpaperama
Sat Dec 27, 2008
4 Comments
1962 Visits
lets say i have a string:
php first characters
$string = "wallpaperama";
and i want to only get the wallpaper part this is how i would show the first 9 letters:
PHP Code:
<?php
$string = "wallpaperama";
$string = substr($string, 0, 9);
echo $string;;
?>
the best way to lear is to try it. open notepad, copy and paste this code and save the file was wallpaperama.php upload to your php site, then open it with your browser.
PHP Code:
<?php
$string = array();
$string [0] = substr("wallpaperama", 0, -1); // returns "wallpaperam"
$string [1] = substr("wallpaperama", 0, 9); // returns "wallpaper"
$string [2] = substr("wallpaperama", 0, -3); // returns "wallpaper"
$string [3] = substr("wallpaperama", 9); // returns "ama"
$string [4] = substr("wallpaperama", -3); // returns "ama"
echo '<pre>';
print_r($string );
echo '</pre>';
?>
if you want to learn more you can visit the official php site:
http://www.php.net/manual/en/function.substr.php
thanks