Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restrict the number of characters in output

Status
Not open for further replies.

buzzt

Programmer
Joined
Oct 17, 2002
Messages
171
Location
CA
I need to use PHP to get the string length [which I already have with strlen()] and trim off any characters after a certain length. In other words, if the word PURPLE was returned from the query, and I wanted to restrict the number of chracters to 4, then the output would actually read PURP... and not PURPLE.

Any ideas?
 
use substr($string, int start [, int length])

where $string is the string to be trimmed

start is an integer that is the starting location (ie 0 for begining of word etc)

length is the length of the string to be taken from the start position

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
I guess with PHP 5 you can use the str_split(), but I found a function for earlier versions.

Function str_split($string, $chunksize=1)
{
preg_match_all('/('.str_repeat('.', $chunksize).')/Uims', $string, $matches);
return $matches[1];
}

$str=PURPLE
$trim=str_split($str, 4);
echo $trim[0]; //This will echo the first 4 characters.


Author:
 
Yours (Bastien) is easier...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top