View Full Version : PHP Limiting Text Characters

05-25-2004, 12:25 AM

I am new here and new to PHP coding. My question is, how do I change this function below from 30 Characters to 20 Characters??? Just change the number 30 to 20??


if (strlen ($str_towrite)>30)
echo substr($str_towrite, 0, strrpos( substr($str_towrite, 0, 30),' ' )) . "...";
echo $str_towrite;

Can someone help me with this function...Please help. Would appreciate...

Kind regards,

Idunno :confused:

05-25-2004, 12:28 AM
Welcome here !

Indeed, just change the 30 to 20 should do it.

05-25-2004, 12:35 AM
Hello raf,

Wow! Thanks for the quick responds! :) I have tryed to change to 20 charecters, and when I upload script, only the "..." in the function will show at the end in the text, and not the wording/text???

Kind regards,

Idunno :(

05-25-2004, 12:47 AM
can you post your edited code?
+ also the value you tested it with.

05-25-2004, 12:49 AM
Thank you for your reply!

Here is a sample pic (attached).



05-25-2004, 12:56 AM
i see.

now, this is not a codingerror. What the code does is

echo substr($str_towrite, 0, strrpos( substr($str_towrite, 0, 20),' ' )) . "...";

so it prints a substring (part of your string), starting from 0 until the last position of a blank, before the 20 character, and then add '...'.
That's the
strrpos( substr($str_towrite, 0, 20),' ' ) part
(start from position 20 and then go back to the start of the string until the first blank)

But your value doesn't contain a blank, so the substr will go from 0 to 0

This function (it's taken from a usercomment i the online manual ... (yes we know ;) )) is used to cut on a whole word, but maybe you don't need that, or maybe you shouldn't need to use that, given the type of values that you have ...

If you just need the first 20 characters, then all you need is

echo substr($str_towrite, 0, 20) . ' ...';

05-25-2004, 01:09 AM
Hi Raf,

I did what you said: echo substr($str_towrite, 0, 20) . ' ...'; and it works fine now!!!!! :thumbsup: :thumbsup: :thumbsup:

Thanks a million guy!! Couldn't do it with out you!!

I am glad their are still people out their like your self that don't mine helping folks like myself. I am glad you were there!!! :) :) :thumbsup: :thumbsup:

Kindest & Warmest Regards,


05-25-2004, 01:32 AM
You're welcome !

Glad i could help :thumbsup: Happy coding!