How to get the last char of a string in PHP?

前端 未结 12 1377
说谎
说谎 2020-12-04 05:24

I need to get the last character of a string. Say I have \"testers\" as input string and I want the result to be \"s\". how can I do that in PHP?

相关标签:
12条回答
  • 2020-12-04 05:40

    A string in different languages including C sharp and PHP is also considered an array of characters.

    Knowing that in theory array operations should be faster than string ones you could do,

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

    However, standard array functions like

    count();
    

    will not work on a string.

    0 讨论(0)
  • 2020-12-04 05:43

    I can't leave comments, but in regard to FastTrack's answer, also remember that the line ending may be only single character. I would suggest

    substr(trim($string), -1)
    

    EDIT: My code below was edited by someone, making it not do what I indicated. I have restored my original code and changed the wording to make it more clear.

    trim (or rtrim) will remove all whitespace, so if you do need to check for a space, tab, or other whitespace, manually replace the various line endings first:

    $order = array("\r\n", "\n", "\r");
    $string = str_replace($order, '', $string);
    $lastchar = substr($string, -1);
    
    0 讨论(0)
  • 2020-12-04 05:43

    Use substr() with a negative number for the 2nd argument.$newstring = substr($string1, -1);

    0 讨论(0)
  • 2020-12-04 05:46

    I'd advise to go for Gordon's solution as it is more performant than 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();
    

    outputs something like

     ---------------------------------- 
     10000000 repetitions...
     ----------------------------------
    
     substr() took 2.0285921096802seconds 
     array access took 1.7474739551544seconds
    
    0 讨论(0)
  • 2020-12-04 05:47

    You can find last character using php many ways like substr() and mb_substr().

    If you’re using multibyte character encodings like UTF-8, use mb_substr instead of substr

    Here i can show you both example:

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

    LIVE DEMO

    0 讨论(0)
  • 2020-12-04 05:49

    Siemano, get only php files from selected directory:

    $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.'<br />';
      }
    }
    
    0 讨论(0)
提交回复
热议问题