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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
?>
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Fantastic! Thanks! :D :D :D


------------------------------------
Happy camper!
Post Reply