Page 1 of 1

String Handling... Shouldn't be that hard.

Posted: Mon Jun 05, 2006 1:13 pm
by JellyFish
Hello, I'm looking for some ways of handling this string I have. Basically I need something like this:

Expressed in plan English

if $mystring has more then 25 characters?
then
cut $mystring down to 22 characters and add string "..." to the end of $mystring


So, that was just a little example of what I'm trying to get at (Note: Think me odd, I am odd, I work in very strange ways).

I'd probably need, I'm not sure, a function that can add "..." at the 22nd character if the string if the length of the string is larger then 25 characters.

The reason I need this is because I don't want a string to be longer then 25 characters, and if it is then make it 22 characters with "..." at the end (thus 25 characters, 22 + "..." = 25).

Hopefully that's enough said, ask questions if I didn't cover anything. Thanks for reading my post. :D

Posted: Mon Jun 05, 2006 1:19 pm
by RobertGonzalez
Cheap and easy hack (though will probably irritate you sooner than later by truncating strings in the middle of a word)...

Code: Select all

<?php
$string_to_trim = "This is some string that is pretty stinking long and needs to be cutted to shorter than it is."

if (strlen($string_to_trim) > 25)
{
    $string_to_trim = substr($string_to_trim, 0, 22) . '...';
}

echo $string_to_trim;
?>

Posted: Mon Jun 05, 2006 1:27 pm
by JellyFish
Fantastic! Thanks! :D :D :D


------------------------------------
Happy camper!