View Full Version : Substr() Help


Coconut99
07-21-2003, 02:49 PM
Thank goodness the boards are back up! I was suffering from withdrawal!

Anyway, I'm working on a script that generates a random name using form fields that the user fills in. (Sort of like a madlib.) I want the PHP file to take the first letter of two of the form fields and then display it, so I thought I would use the substr() function. It's not working.

teams.html:

<FORM name="team" method="post" action="process_teams.php">
Your Full Name: <INPUT TYPE="text" name="name"><BR>
Animal: <INPUT TYPE="text" name="animal"><BR>
Your Hometown: <INPUT TYPE="text" name="location"><BR>
Color: <INPUT TYPE="text" name="color1"><BR>
Different Color: <INPUT TYPE="text" name="color2"><BR><BR>
<center><INPUT TYPE="submit" value="Generate!"><BR></center>
</FORM>

process_teams.php:

<?
$letter1 = substr("$location, 0");
$letter2 = substr("$animal, 0");
?>
The <? print("$location"); ?> <? print("$animal"); ?>s!<BR>
Colors: <? print("$color1"); ?> and <? print("$color2"); ?><BR>
Captain: <? print("$name"); ?><BR>
Emblem: <? print("$color1"); ?> <? print("$letter1"); ?> and <? print("$letter2"); ?> printed side-by-side on back of jersey.


When I run that, I get this:
Warning: Wrong parameter count for substr() in /home/virtual/site140/fst/var/www/html/php/process_teams.php on line 2

Warning: Wrong parameter count for substr() in /home/virtual/site140/fst/var/www/html/php/process_teams.php on line 3

What's wrong?! Please help! Thanks!

Dude128
07-21-2003, 02:58 PM
your parameters are wrong. the string is the only thing that should have quotes around it, like so: substr("$location",0)

also, if you only want the first character, you need to specify the length. what you have is taking the entire string, since you're starting it at position 0 ;)

substr("$location",0,1) should be what you want :)

Coconut99
07-21-2003, 03:10 PM
Thanks so much! It works now. :)