input check
Moderator: General Moderators
input check
I want to check the user's input and if it's bigger then, say, 15 characters {... }
how can I do that?
how can I do that?
Code: Select all
<?php
$input_lenght = strlen($user_input);//$user_input from a form maybe?
if($input_lenght > 15)
{
//do stuff
}
?>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) {
$dNum = intval($num);
if (strlen($varb) > $dNum) {
$nVarb = substr($varb, 0, $dNum);
$nVarb .= '...';
}
elseif(strlen($varb) < $dNum) {
$nVarb = $varb;
}
return $nVarb;
}
// 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.
same thing, less code?
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
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>-
Stoneguard
- Forum Contributor
- Posts: 101
- Joined: Wed Aug 13, 2003 9:02 pm
- Location: USA
-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
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.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
If it has to be secure or your system relys on some assumptions you have to test everything and anything.