I know this is very basic, but it seems to be a common question that people ask me: "how to truncate a string in PHP" -- basically, how do I cut a string after a certain length?
Well it's simple, I created a function for you all to use if you wish that will cut a string off after a certain length that you choose.
The usage is very easy and is demonstrated in the code snippet below:
I hope that this helps you if you choose to use it.
Mark.
Well it's simple, I created a function for you all to use if you wish that will cut a string off after a certain length that you choose.
The usage is very easy and is demonstrated in the code snippet below:
PHP:
<?php
function truncate( $str, $allowed_length=10 )
{
return substr( trim( $str ), 0, $allowed_length ); //return the truncated value
}
echo truncate( "Mark Eriksson is the best person ever!" ); //output: Mark Eriks
echo truncate( "Mark Eriksson is the best person ever!", 25 ); //output: Mark Eriksson is the best
?>
I hope that this helps you if you choose to use it.
Mark.