PDA

View Full Version : PHP and Strings



Christopher
April 19th, 2002, 18:04
Lets say you had a form... On this form was a texbox...

Now, say that you passed the form through a script (using method="post")

How would you - for example - return the 11 letter of the string?

I dont know if I made this clear, but thanx!

spec
April 19th, 2002, 19:36
have a textbox
<input type="text" name="foo">


on your PHP script


<?
$foo = @$HTTP_POST_VARS['foo'];
echo $foo;
?>


now if you wanted the string length


$foo_length = strlen($foo);


and if you wanted to limit user input to 11 characters


if(strlen($foo)>11)
{
echo "Sorry but your input is too long";
exit();
}

Christopher
April 19th, 2002, 20:06
No, I meant to GET the 11 character and lets say, print it to the screen...

Dusty
April 19th, 2002, 21:00
print $variable{11};

Christopher
April 19th, 2002, 21:32
Originally posted by Dusty
print $variable{11};
Seriously? Cool, thanx!

Christopher
April 20th, 2002, 12:28
I suppose theres not a function to write a string backwards is there?

ashben
April 20th, 2002, 13:40
Originally posted by Christopher
I suppose theres not a function to write a string backwards is there?

strrev()

Christopher
April 20th, 2002, 13:55
Oh, never thought there would be...

Anyway, I did it without the strrev() function, like this:


$length = strlen($Sentence);
while($length != 0)
{
print $Sentence{$length};
$length--;
}

And it always leaves out the last character. For example, the sentice would be:

I am cool

It would print:

looc ma

The I is missing (the space is there)

To see what I did go here (http://ugz.milescape.com/backwords.html)

I just want to know how to fix this bug, other than telling the user to put a space at the beginning.

ashben
April 21st, 2002, 00:24
Correct the while loop with:

while ($length > -1)