input check

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Northern
Forum Newbie
Posts: 2
Joined: Fri Oct 17, 2003 9:02 pm
Contact:

input check

Post by Northern »

I want to check the user's input and if it's bigger then, say, 15 characters {... }

how can I do that?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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; 
?>
Last edited by Kriek on Mon Oct 20, 2003 7:42 am, edited 2 times in total.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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);
?>
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

lol...i think SG has the best solution for this problem!
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Agreed - assuming input IS via an HTML form.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Well, if he is looking to add elipses, my solution doesnt work real well ( I was speed reading the first post).
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply