17 May
Get first n characters from string
If you want to get simple character from string then can use php default substr function which is quite simple. Belo is the syntax
string substr ( string $string , int $start [, int $length ] )
here
$string is the input string
$start is the non-negative and negative  number. If start is negative means start from the end of string means reverse . If string is non-negative number means start from starting postion.
$length is length of string.
Example
$rest = substr("abcdef", -1); // returns "f"
Now what if you want to number of word from a sentence ?
Here I am doing with simple function with the help of php wordwrap and explode function.
My custom function is
// php function to get word string with desire lenth function getSubStrWdLenght($string,$length=10) { //check if any blank space with string $string = trim($string); // check if string is not smaller then given length default is 10 if(strlen($string) > $length) { $string = wordwrap($string, $length); $string = explode("\n", $string, 2); $string = $string[0] ; } return $string; }
You simply can pass your string and check result like
$string= "We all are not alone"; $result = getSubStrWdLenght($string,1); echo'===>'.$result;
It should return “We”
I am a software engineer who specializes in Internet applications. I have worked with a wide variety of technologies and programming languages to open source LAMP environments. I have more than 6 years of object-oriented programming experience and am highly proficient in ActionScript, PHP, MYSQL, JavaScript, Jquery and a multitude of other technologies used in modern web applications.
Follow me
Latest posts by Rajeev Achra (see all)
- Jquery webcam plugin - June 19, 2016
- How To Add and Delete Users on a CentOSServer - June 5, 2016
- How To Set Up vsftpd on CentOS 6 - June 5, 2016