How To Get The First 2 Characters In A String With Php Substr

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. Last Upated: Thu May 15, 2014
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

chico
dana
this is exactly what i was looking for

thanks
gerrit
thanks! easy and clear!
Aamir
Thanks for sharing your approach! Your post helped me allot this worked like a charm.
Title: How To Get The First 2 Characters In A String With Php Substr [1235]
Tags: how to get the first 2 characters in a string
By
Comments 4
Visits 1759