Page 1 of 1

input check

Posted: Fri Oct 17, 2003 9:02 pm
by Northern
I want to check the user's input and if it's bigger then, say, 15 characters {... }

how can I do that?

Posted: Fri Oct 17, 2003 9:26 pm
by qads

Code: Select all

<?php
$input_lenght = strlen($user_input);//$user_input from a form maybe?
if($input_lenght > 15)
{
//do stuff
}
?>
read on strlen(); at php.net.

Posted: Fri Oct 17, 2003 9:30 pm
by Kriek
Very simplistic script/function intended to limit the character output of a given string and emphasize that the string does indeed continue. Which subsequently happens to utilize the strlen() function as qads previously mentioned.

Code: Select all

<?php 
    /* 
    kriek at phpfreaks dot com 
    */ 
    function chopSent($varb, $num) &#123; 
        $dNum = intval($num); 
        if (strlen($varb) > $dNum) &#123; 
            $nVarb = substr($varb, 0, $dNum); 
            $nVarb .= '...'; 
        &#125; 
        elseif(strlen($varb) < $dNum) &#123; 
            $nVarb = $varb; 
        &#125; 
        return $nVarb; 
    &#125; 
    // Usage of function 
    $theSent = 'PHP is a widely-used general-purpose scripting language'; 
    $theSent = chopSent($theSent, 15); 
    echo $theSent; 
?>

Posted: Fri Oct 17, 2003 9:38 pm
by qads
same thing, less code? :P

Code: Select all

<?php
function cut_it($str, $num)
{
$length = strlen($str);
$return = $str;
if($length > $num)
{
$return = substr($str, -$length, $num)."...";
}
return $return;
}

//to use it
echo cut_it("PHP is a widely-used general-purpose scripting language", 10);
?>

Posted: Fri Oct 17, 2003 9:44 pm
by Stoneguard
Well, if you would rather limit what they can enter in the first place, you only need one parameter:

Code: Select all

<input type=text name=thisfield maxlength=15>

Posted: Fri Oct 17, 2003 9:46 pm
by qads
lol...i think SG has the best solution for this problem!

Posted: Fri Oct 17, 2003 9:49 pm
by Kriek
Agreed - assuming input IS via an HTML form.

Posted: Fri Oct 17, 2003 9:55 pm
by Stoneguard
Well, if he is looking to add elipses, my solution doesnt work real well ( I was speed reading the first post).

Posted: Sat Oct 18, 2003 6:55 am
by volka
<input type=text name=thisfield maxlength=15>
if strlen<=15 is vital you have to check it server-side. clients should recognize the maxlength property but you cannot force them to do so.

Posted: Sat Oct 18, 2003 7:04 am
by Cruzado_Mainfrm
what if i download the html page and change the code from maxlength=15 to maxlength=255 and submit something :D?
isn't that a problem? check in server-side too

you can also check if the server is the same as one in your code, so that way u don't have to check for every value

Posted: Sat Oct 18, 2003 8:32 am
by volka
you can also check if the server is the same as one in your code, so that way u don't have to check for every value
that's also something not trustworthy if you refer to HTTP_REFER. The value is act of courtesy by the client and can be forged, too.
If it has to be secure or your system relys on some assumptions you have to test everything and anything.