Finding a character at a specific position of a string

前端 未结 5 1708
眼角桃花
眼角桃花 2021-01-05 14:28

Since I am still new to PHP, I am looking for a way to find out how to get a specific character from a string.

Example:

$word = \"master\";
$length =         


        
5条回答
  •  青春惊慌失措
    2021-01-05 14:58

    Use substr

    $GetThis = substr($myStr, 5, 5);
    

    Just use the same values for the same or different if you want multiple characters

    $word = "master";
    $length = strlen($word);
    $random = rand(0,$length-1);
    $GetThis = substr($word, $random, $random);
    

    As noted in my comment (I overlooked as well) be sure to start your rand at 0 to include the beginning of your string since the m is at place 0. If we all overlooked that it wouldn't be random (as random?) now would it :)

提交回复
热议问题