...

View Full Version : PHP Limiting Text Characters



idunno
05-24-2004, 11:25 PM
Hello,

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??

<?
$str_towrite=$rst["product_name"];

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

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

Kind regards,

Idunno :confused:

raf
05-24-2004, 11:28 PM
Welcome here !

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

idunno
05-24-2004, 11:35 PM
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 :(

raf
05-24-2004, 11:47 PM
can you post your edited code?
+ also the value you tested it with.

idunno
05-24-2004, 11:49 PM
Thank you for your reply!

Here is a sample pic (attached).

Thanks!:)

Idunno

raf
05-24-2004, 11:56 PM
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) . ' ...';

idunno
05-25-2004, 12: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,

Idunno

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

Glad i could help :thumbsup: Happy coding!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum