Jump to content

second letter of a text field


Go to solution Solved by Psycho,

Recommended Posts

 I know the problem is here

I cant understand what to put in there to get the value of the second character when something is typed into the text field

also how could I acces the value of $str in my print statement?

Please and thank you ..

echo substr($str, 3,-1); 

f (filter_has_var(INPUT_POST, "color")) {
  
  //work with the form
      		$color = filter_input(INPUT_POST, "color");
      		$str = filter_input(INPUT_POST, "color"); 
echo substr($str, 3,-1);

      		  print"<h2>
Your word was $color  </h2>";
Link to comment
https://forums.phpfreaks.com/topic/280112-second-letter-of-a-text-field/
Share on other sites

  • Solution

Did you read the manual for substr()? It's pretty straightforward for what you want.

substr($str, 1, 1);

The first number represent the index of the character yuo want to start at (first character is at index 0, so the 2nd is at index 1). The second number represents the number of characters you want to obtain.

Edited by Psycho

To add more to the above method kicken provided, strings should be immutable "objects", means that you can read their value like an array, but you cannot set the index to something else.

 

Example.

 

<?php
$str = "This is an example string.";
$splitstr = $str[0]; // "T"
$str[0] = "D"; // fails
echo($str[0]); // still displays "T"
?>

To add more to the above method kicken provided, strings should be immutable "objects", means that you can read their value like an array, but you cannot set the index to something else.

Whether they should be immutable or not is debatable. The fact is that they are not however, you can change individual characters through array access.

<?php
$str = 'Blah';
var_dump($str[1]);
//string(1) "l"

$str[1] = 'a';
var_dump($str[1], $str);
//string(1) "a"
//string(4) "Baah"
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.