SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    str_replace alternate blank space

    hi all

    i can replace every " " blank space with "," str_replace

    PHP Code:
    $pn=$row_ot['product_name'];
    $pn str_replace(" "","$pn); 
    but if i want to replace every "alternate blank space" then what will be the solution.

    Means i want to leave first blank space as it is and would like to replace the second blank space with ",".

    vineet

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $pn=$row_ot['product_name'];
    $i = -1$offset 0;
    while(
    $offset strpos($pn' '$offset))
    {
       
    $i++;
       if (
    $i % 2 == 0) $pn = substr_replace($pn, ',', $offset, 1);
       
    $offset++;
    }
    print 
    $pn
    The weird &# 37 ; thingy is supposed to be %, percentage sign, used for modulo
    Last edited by ScallioXTX; Aug 10, 2009 at 22:51. Reason: The weird % is supposed to be %, percentage sign, used for modulo
    Rémon - Hosting Advisor

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

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi scallio

    i used your code
    PHP Code:
    $pn="sony ericsson w995 black";
    $i = -1
    $offset 0;
    while(
    $offset strpos($pn' '$offset))
    {   
    $i++;   
    if (
    $i == 0
    $pn substr_replace($pn','$offset1);   
    $offset++;
    }
    print 
    $pn
    it outputs as
    Code:
    sony,ericsson w995,black
    but i want to output it as
    Code:
    sony ericsson, w995 black
    vineet

  4. #4
    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 can't say I'm entirely happy with it, but here's a RegExp solution.
    PHP Code:
    <?php
    $sString 
    'A string with many spaces in it.';
    preg_match_all(
        
    '~\s[^\s]*(\s)~',
        
    $sString,
        
    $aMatches,
        
    PREG_OFFSET_CAPTURE
    );
    foreach(
    $aMatches[1] as $aMatch)
    {
        
    $sString[$aMatch[1]] = ',';
    }
    echo 
    $sString#A string,with many,spaces in,it.
    ?>
    Here's a more standard approach too.
    PHP Code:
    <?php
    $sString 
    'A string with many spaces in it.';
    for(
    $iStringPosition $iSpaceCount 0$iStringPosition strlen($sString); $iStringPosition++)
    {
        if((
    ' ' === $sString[$iStringPosition]) && (=== ($iSpaceCount++ % 2)))
        {
            
    $sString[$iStringPosition] = ',';
        }
    }
    echo 
    $sString#A string,with many,spaces in,it.
    ?>
    I much prefer the latter.
    @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.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by vinpkl View Post
    hi scallio

    i used your code
    PHP Code:
    $pn="sony ericsson w995 black";
    $i = -1
    $offset 0;
    while(
    $offset strpos($pn' '$offset))
    {   
    $i++;   
    if (
    $i &#37; 2 == 0) 
    $pn substr_replace($pn','$offset1);   
    $offset++;
    }
    print 
    $pn
    it outputs as
    Code:
    sony,ericsson w995,black
    Oops, $i should start at 0 ...

    So

    PHP Code:
    $pn="sony ericsson w995 black";
    $offset $i 0;
    while(
    $offset strpos($pn' '$offset))
    {   
       
    $i++;   
       if (
    $i == 0
       
    $pn substr_replace($pn','$offset1);   
       
    $offset++;
    }
    print 
    $pn// sony ericsson,w995 black 
    Rémon - Hosting Advisor

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

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    What about using a different regular expression to look for, and replace, every second space?

    PHP Code:
    $pn "sony ericsson w995 black with apples";
    echo 
    preg_replace('/( \w+)\K /'', '$pn); 
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    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)
    Nice Salathe, although I'm struggling to find documentation for '\K'.

    I don't suppose you have a link do you?
    @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.

  8. #8
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    What about using a different regular expression to look for, and replace, every second space?

    PHP Code:
    $pn "sony ericsson w995 black with apples";
    echo 
    preg_replace('/( \w+)\K /'', '$pn); 
    thanks salathe

    this is really a nice short solution.

    vineet

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Nice Salathe, although I'm struggling to find documentation for '\K'.

    I don't suppose you have a link do you?
    Sure, clicky clicky (there's no direct link, but it's the last paragraph before the user notes).

    Quote Originally Posted by vinpkl View Post
    thanks salathe

    this is really a nice short solution.

    vineet
    You're welcome.
    Salathe
    Software Developer and PHP Manual Author.

  10. #10
    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)
    Quote Originally Posted by Salathe View Post
    Sure, clicky clicky (there's no direct link, but it's the last paragraph before the user notes).
    Quote Originally Posted by Da Manual
    \K can be used to reset the match start since PHP 5.2.4. For example, the pattern foo\Kbar matches "foobar", but reports that it has matched "bar". The use of \K does not interfere with the setting of captured substrings. For example, when the pattern (foo)\Kbar matches "foobar", the first substring is still set to "foo".
    Great, thanks Salathe.
    @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.


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
  •