SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Want to replace the last occurance !

    Hello

    I have a string with few words. I want to replace the last occurrence of [SPACE] with [COMMA][SPACE]

    Means suppose this is my string :

    Code:
    word1 word2 word3 word4 word5
    will be changed to :

    Code:
    word1 word2 word3 word4, word5
    Please give me some hint how to do this !

    Thanks

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,082
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $str 'word1 word2 word3 word4 word5';
    $str substr_replace($str', 'strrpos($str' '), 1);
    print 
    $str// word1 word2 word3 word4, word5 

    See:
    http://nl3.php.net/manual/en/functio...tr-replace.php

    and

    http://nl3.php.net/manual/en/function.strrpos.php
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    echo preg_replace(
        
    '~(\\s)(?=\\w+$)~',
        
    ', ',
        
    'word1 word2 word3 word4 word5'
    ); #word1 word2 word3 word4, word5
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh thanks a lot both of u are great !


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
  •