Forum Moderators: coopster

Message Too Old, No Replies

How to Limit Text Characters in PHP?

How to Limit Text Characters in PHP?

         

JackieB

2:12 am on Apr 4, 2005 (gmt 0)

10+ Year Member



I've searched previous threads. Most mention a way to limit character input by setting parameters in phpmyadmin or SQL database.

I have these options, but have never used them.

Isn't there a way to just add a command to this string?

<tr>
<td width="150" align="right" valign="top"><strong><?php echo"$Submit_LinkDescription";?></strong></td>
<td width="350" colspan="2" align="left" bgcolor="#FFFFFF">
<textarea name="desc" cols="40" rows="6" wrap="VIRTUAL" id="desc"></textarea>
</td>
</tr>
<tr>

ironik

2:47 am on Apr 4, 2005 (gmt 0)

10+ Year Member



If this input is part of a form, you can limit the number of characters permitted by using strlen() after the form is submitted.

e.g.
$charLimit = 500;
if (strlen($_POST['desc']) <= $charLimit)
{
// do something
} else {
// do error
}

I don't think you can limit the number of characters you input into the actual textarea html, unless you dodgy it up with some javascript, even then it can be surpassed easily.

dreamcatcher

7:43 am on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have a preview function, you can use substr() to display only the amount of characters you want. Or just use is to insert the limit, whichever you choose:

$desc = $_POST['desc'];

$text = substr($desc,0,100);

echo $text;

This would show only the first 100 characters.

dc