首页 文章

如何在PHP中获取字符串的最后一个字符串?

提问于
浏览
392

我需要获取字符串的最后一个字符 . 假设我有“测试者”作为输入字符串,我希望结果为“s” . 我怎么能用PHP做到这一点?

11 回答

  • 25

    请记住,如果您使用 fgets() 函数从文本文件中读取一个字符串,则需要使用 substr($string, -3, 1) 以便获得实际字符而不是CRLF(回车符换行符号)的一部分 .

    我不认为提出这个问题的人需要这个,但对我来说,我无法从文本文件中的字符串中获取最后一个字符,因此我确信其他人会遇到类似的问题 .

  • 808

    Siemano,只从所选目录中获取php文件:

    $dir    = '/home/zetdoa/ftp/domeny/MY_DOMAIN/projekty/project';
    $files = scandir($dir, 1);
    
    
    foreach($files as $file){
      $n = substr($file, -3);
      if($n == 'php'){
        echo $file.'
    '; } }
  • 3

    或者direct string access

    $string[strlen($string)-1];
    

    请注意,这不适用于多字节字符串 . 如果需要使用多字节字符串,请考虑使用mb_* string family of functions.

    从PHP 7.1.0开始,也支持负数数字索引,例如 $string[-1];

  • 66

    在即将推出的PHP 7.1中,你将能够做到这一点(Accepted rfc for negative string offsets):

    <?php
    $silly = 'Mary had a little lamb';
    echo $silly[-20];
    echo $silly{-6};
    echo $silly[-3];
    echo $silly[-15];
    echo $silly[-13];
    echo $silly[-1];
    echo $silly[-4];
    echo $silly{-10};
    echo $silly[-4];
    echo $silly[-8];
    echo $silly{3};
    die();
    

    我会让你猜出输出 .

    另外,我将这个结果添加到xenonite's性能代码中:

    substr()花了7.0334868431091seconds数组访问花了2.3111131191254seconds直接字符串访问花了1.7971360683441seconds

  • 59

    我建议去寻找Gordon的解决方案,因为它比substr()更高效:

    <?php 
    
    $string = 'abcdef';
    $repetitions = 10000000;
    
    echo "\n\n";
    echo "----------------------------------\n";
    echo $repetitions . " repetitions...\n";
    echo "----------------------------------\n";
    echo "\n\n";
    
    $start = microtime(true);
    for($i=0; $i<$repetitions; $i++)
        $x = substr($string, -1);
    
    echo "substr() took " . (microtime(true) - $start) . "seconds\n";
    
    $start = microtime(true);
    for($i=0; $i<$repetitions; $i++)
        $x = $string[strlen($string)-1];
    
    echo "array access took " . (microtime(true) - $start) . "seconds\n";
    
    die();
    

    输出类似的东西

    ---------------------------------- 
     10000000 repetitions...
     ----------------------------------
    
     substr() took 2.0285921096802seconds 
     array access took 1.7474739551544seconds
    
  • 1
    substr($string, -1)
    
  • 16

    您可以使用php找到最后一个字符,如substr()mb_substr() .

    如果您使用UTF-8等多字节字符编码,请使用mb_substr而不是substr

    在这里,我可以向您展示两个例子:

    <?php
        echo substr("testers", -1);
        echo mb_substr("testers", -1);
    ?>
    

    LIVE DEMO

  • 5

    我不能留下评论,但关于FastTrack的答案,还要记住,行结尾可能只是单个字符 . 我会建议

    substr(trim($string), -1)
    

    EDIT: 我的代码是由某人编辑的,因为它没有按照我的指示行事 . 我已恢复原始代码并更改了措辞以使其更清晰 .

    trim (或 rtrim )将删除所有空格,因此如果您确实需要检查空格,制表符或其他空格,请先手动替换各行结尾:

    $order = array("\r\n", "\n", "\r");
    $string = str_replace($order, '', $string);
    $lastchar = substr($string, -1);
    
  • 3

    包含C sharp和PHP的不同语言的字符串也被视为字符数组 .

    知道理论上阵列操作应该比你能做的字符串操作更快,

    $foo = "bar";
    
    
    $lastChar = strlen($foo) -1;
    echo $foo[$lastChar];
    
    $firstChar = 0;
    echo $foo[$firstChar];
    

    但是,标准数组函数如

    count();
    

    不会对字符串起作用 .

  • 1
    substr("testers", -1); // returns "s"
    
  • -2

    从PHP 7.1.0开始,还支持负字符串偏移 . 所以,如果你跟上时间的步伐,你可以像这样访问字符串中的最后一个字符:

    $str[-1]
    

    DEMO

相关问题