SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: String Problem

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Wales
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String Problem

    How do you select the last character of a string?

    Thanks

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    substr($str ,-1) ;

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Wales
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks firepages

  4. #4
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Gainesville, Fl
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks firepages!

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by firepages
    substr($str ,-1) ;
    Well... that is the nice boring high performance way to do it , but if you need some code obfuscation techniques, how about one of these:

    PHP Code:
    $str 'last';

    echo 
    $str[strlen($str)-1];

    preg_match('/(.)$/'$str$match);
    echo 
    $match[1];

    echo 
    array_pop(preg_split('//',$str, -1PREG_SPLIT_NO_EMPTY));

    echo 
    array_pop(array_diff(explode("\n",chunk_split($str,1)),array(''))); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Wales
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be a pain but how would I use substr($str ,-1) ; to remove the last character of a string?

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by L30N
    Sorry to be a pain but how would I use substr($str ,-1) ; to remove the last character of a string?
    You would not, that is a different questions, whose answer is
    PHP Code:
    substr($str0, -1); 

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Wales
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

  9. #9
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh ok , having seen the error of my ways, may I instead suggest ... instead of sweatje's , to be honest , quite boring answer

    PHP Code:
     <?php
     
    echo preg_replace
         
    "/".array_reduce
             
    explode"~" ,wordwrap$str 1,"~",) ) , 
             
    create_function'$k,$v','return $v;' ) )."$/" ,
         
    '' ,
         
    $str
     
    );
     
    ?>

  10. #10
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How could you possibly do something as inefficient as using double quotes on a string which does not require variable replacement

    PHP Code:
    $str 'last';
    eval(
    unserialize(gzuncompress(base64_decode('eJw9jM0KgzAQhO8+xbIEEmFRciqYh5EQtz9UkrBGbS999kaonbkMH3yzDPZiB+RwT5CFb6Nwnn1g00AN9th5Ef+ueFpPeoRfeU4TG8APAu1Jpl18NqCWIkBg
    qXKy0NbSXwrCvvB4XWMojxQNaPUktWnSwmWVCGpz+nA6VD3+RDzHcd20Dt0X2AYu2g=='
    )))); 
    You have to get rid of those spaces in the string that the forum places in there if you try it out.

  11. #11
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm ... but at least I left the replacement argument as single quotes ... perhaps someone was too lazy to escape them ...

    s:171:"~~~~ "" , $str );";



    but yes I admit defeat

  12. #12
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by firepages
    ... perhaps someone was too lazy to escape them ...
    Guilty as charged, I hate the "leaning toothpick" syndrome, even to the point of introducing ineffeciencies in order to have "pretty" code


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •