Finding a character at a specific position of a string

前端 未结 5 1683
眼角桃花
眼角桃花 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 15:12

    You can use your string the same like 0-based index array:

    $some_string = "apple";
    echo $some_string[2];
    

    It'll print 'p'.

    or, in your case:

    $word = "master";
    $length = strlen($word);
    $random = rand(0,$length-1);
    
    echo $word[$random];
    

提交回复
热议问题