SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed on modifying string

    I have a string like this

    Code:
    xbox 360 video locks after 2 minutes xbox 360 video locks after 2 minutes
    
    I want to put comma after every two words like this.
    
    xbox 360, video locks, after 2, minutes xbox, 360 video, locks after, 2 minutes


    PLEASE HELP!!!

    Thanks
    -PARASH

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    You could use a regular expression to look for two words and add a comma after each group of two words.

    PHP Code:
    $subject 'xbox 360 video locks after 2 minutes xbox 360 video locks after 2 minutes';
    $separated preg_replace('/\w++\s++\w++(?!$)/''$0,'$subject);

    echo 
    $separated
    Salathe
    Software Developer and PHP Manual Author.

  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)
    I'm sure the expression could be tweaked, and preg_split would probably be better suited but this works...
    PHP Code:
    <?php
    preg_match_all
    ('~([a-z0-9]+\s?){2}\s?~i''xbox 360 video locks after 2 minutes xbox 360 video locks after 2 minutes'$aMatches);
    echo 
    implode(', 'array_shift($aMatches)); #xbox 360 , video locks , after 2 , minutes xbox , 360 video , locks after , 2 minutes
    ?>
    Edit: Bugger, Salathe beat me to it. Damn my poor RegExp skills.
    @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
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Quote Originally Posted by SilverBulletUK View Post
    Edit: Bugger, Salathe beat me to it. Damn my poor RegExp skills.
    Always good to see different approaches to problems.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Off Topic:


    Always good to see different approaches to problems.
    An other solution, but without RegEx.
    I doubted about posting it since you already answered. But since it is ok to have multiple solutions:

    PHP Code:
    $str 'xbox 360 video locks after 2 minutes xbox 360 video locks after 2 minutes';
    $newStr = (string) '';
    $words explode(' '$str);

    foreach (
    $words as $key => $word) {
        
    $newStr .= ($key != && $key == 0) ? ', ' ' ';
        
    $newStr .= $word;
    }

    echo 
    $newStr
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys.!!!


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
  •