View Full Version : Truncate Variable
rogersnm
08-02-2006, 09:13 AM
I have searched far and wide but can not find the answer xD
ok, so, how do i truncate (trim) characters off a string so it could have been x characters before but is 10 now?
Regards,
Nick.
Paul M
08-02-2006, 09:28 AM
<a href="http://uk.php.net/substr" target="_blank">substr()</a>
rogersnm
08-02-2006, 09:31 AM
so:
substr($str, 0, 5); will limit it to 5 characters
is there an if so if it is more than x characers then do this and do this eg:
if ($str something)
{
substr($str, 0, 5);
$str = $str . "...";
}
Paul M
08-02-2006, 09:36 AM
Yes (but no quotes needed around $str).
rogersnm
08-02-2006, 09:44 AM
ok but what about the second bit?
Paul M
08-02-2006, 09:51 AM
Second bit ?
rogersnm
08-02-2006, 10:01 AM
if ($str something)
{
substr($str, 0, 5);
$str = $str . "...";
} What do i put for if ($str something) to only do that if the string is more than 5 characters long?
Guest190829
08-02-2006, 10:30 AM
if ($str something)
{
substr($str, 0, 5);
$str = $str . "...";
} What do i put for if ($str something) to only do that if the string is more than 5 characters long?
if (strlen($str) > 5)
{
//string is greater than 5 chars
}
Also whitespace counts, so you may want to think about trimming the whitespace from the string beforehand. :)
And here is a list of all php string functions, which could come in handy:
http://us2.php.net/manual/en/ref.strings.php
rogersnm
08-02-2006, 10:30 AM
thanks :)
ok so i have:
if (strlen($posttitle) > 30)
{
substr($posttitle, 0, 30);
$posttitle = $posttitle . "...";
}
if (strlen($postforumname) > 30)
{
substr($postforumname, 0, 30);
$postforumname = $postforumname . "...";
} but the variables just show up empty
Paul M
08-02-2006, 10:36 AM
You have to assign the substr to something.
$var = substr(.....)
rogersnm
08-02-2006, 10:59 AM
i have, i realised the problem, i put $post[title] = $postitle rather than the other way around.
it's not actually working, my code is: $posttitle = $post['title'];
$postforumname = $post['forumname'];
if (strlen($posttitle) > 16)
{
substr($posttitle, 0, 17);
$posttitle = $posttitle . "...";
}
if (strlen($postforumname) > 16)
{
substr($postforumname, 0, 17);
$postforumname = $postforumname . "...";
}
$post['title'] = $posttitle;
$post['forumname'] = $postforumname; but all it actually does is add "..." to the end of the string if it is more than 16 characters instead of cutting it to 17 and then adding it on the end.
ah i see it, i didn't put "$var = " before it.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.