PDA

View Full Version : Help with a script


Phocus
Mon 13th Aug '01, 7:23pm
Ok i wrote this function deal, but i cant get it to work right, ill go over what it does.

It takes in 3 different variables, the string name to use, the maximum length and the preceding type.

The function takes the string given, and it only shows the number of characters that are supplied when the function is used (this includes the length of the "mark up") for example
$string = "ABCDEFGHIJKLMNOP";
$length = "8";
$markup = "...";
$string2 = strlcheck($string, $length, $markup);
echo("$string2");
and it would output "ABCDE..."

But it isnt working the way i hoped. Heres my code:



function strlcheck($stringname, $maximumlength, $precedingargument="") {
$stringlength = strlen($stringname);
$precedinglength = strlen($precedingargument);
if ($stringlength > $maximumlength) {
$nmbr2 = 0;
$doublelength = $stringlength - $precedinglength;
while($nmbr2 <= $doublelength)
{
echo("$stringname[$nmbr2]");
$nmbr2 = $nmbr2 + 1;
}
echo("$precedingargument");
}
if ($stringlength <= $maximumlength) {
echo("$stringname");
}
return;
}


$teststring = "1234567891011";
$length = 5;
$prec = "...";
echo("The Test String Is: <i>$teststring</i><br><br>");
strlcheck($teststring, $length, $prec);



if you can see what im doing wrong please help, thank you

Mark Hensler
Mon 13th Aug '01, 9:46pm
I just wrote one from scratch... Quicky Code (untested)

I maintained the number and order of your arguements. That way, if you have the function in use anywhere, you don't have to go back and edit them.
function strlcheck($string1, $maxlength, $string2="") {
if ((strlen($string1)-strlen($string2)) > $maxlength) {
$string1 = substr($string1,0,($maxlength-strlen($string2)));
$string1 .= $string2;
}
echo $string1;
//return $string1;
}I think idealy, you'd return the string. In your previous function, you just printed it, so that's what I did.