Re: [PHP] Truncate String
Why do you use trim? (I know what it does, wanna know 'why' you think I don't want white-space?)
Re: [PHP] Truncate String
would be nice if you add the white-spaces removal like this:
PHP Code:
function truncate( $str, $allowed_length=10,$remove_spaces=false )
false by default
Re: [PHP] Truncate String
Your code wraps substr() by supplying two of its parameters for the user, one of which is completely arbitrary would be overridden in almost every scenario anyway. With the added overhead of making two unnecessary function calls -- one for the wrapper and one for trim() -- I can't imagine how a function like this could be any less useful.
Re: [PHP] Truncate String
well, why dont you make one then? :):
Re: [PHP] Truncate String
There's no need to wrap substr() to begin with.
Re: [PHP] Truncate String
well, I have to agree with you in that.
Re: [PHP] Truncate String
I like the idea of the $remove_spaces, that's a good idea.
Made a little expansion on it and included SheenBRs idea:
PHP Code:
<?php
function truncate( $str, $allowed_length=10, $remove_spaces=false, $after_str="" )
{
/*
$str - the string to truncate
$allowed_length - the amount of characters to limit to
$remove_spaces - use trim() to remove white-space
$after_str - another string to appear after the truncation, could be '...' if you like, so you could have a snippet of a story with "..." at the end
*/
$str = ( $remove_spaces == true ) ? trim( $str ) : $str;
$return = substr( $str, 0, $allowed_length );
if ( !empty( $after_str ) ) $return .= $after_str;
return $return;
}
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
echo truncate( "Mark Eriksson is the best person ever!", 25, false, "..." ); //output: Mark Eriksson is the best...
echo truncate( " Mark Eriksson is the best person ever! ", 20, true, "..." ); //output: Mark Eriksson is the...
?>
Re: [PHP] Truncate String
As the others have pointed out, what is the purpose of writing a function that is simply a wrapper around a pre-existing function? That would be like me writing
Code:
def write(message) puts message end
and then using
instead of
Pointless... Just stick with using substr; That's what it was made for. Also, why would you set the default length to 10? Who would want a method that lets you grab only the first 10 characters of a string? I understand you're trying to get better with PHP and you're trying to help people, but this doesn't help anyone. I appreciate the gesture, though.
BTW, you don't need to add the "== true" when using a boolean in a conditional. Simply using "if ($my_bool)" works fine and is cleaner.
Re: [PHP] Truncate String
Quote:
Originally Posted by
Yamachi
As the others have pointed out, what is the purpose of writing a function that is simply a wrapper around a pre-existing function? That would be like me writing
Code:
def write(message) puts message end
and then using
instead of
Pointless... Just stick with using substr; That's what it was made for. Also, why would you set the default length to 10? Who would want a method that lets you grab only the first 10 characters of a string? I understand you're trying to get better with PHP and you're trying to help people, but this doesn't help anyone. I appreciate the gesture, though.
BTW, you don't need to add the "== true" when using a boolean in a conditional. Simply using "if ($my_bool)" works fine and is cleaner.
I only used 10 characters as the default because the string I had used "Mark Eriksson blah blah" wasn't that long, so I chose 10 instead.
I understand that 'if ( $my_bool )' works the same -- Lord knows what made me put == true lol