preg_replace pattern help

Discussion in 'PHP' started by freerollers, Aug 25, 2008.

  1. #1
    can anyone help me how i should write the pattern

    i want to replace "xyz" with "yyy" but only if xyz stands alone. Also it should be case-insensitive.

    "xyz what go www.xyz.com XyZ test"
    ->
    "yyy what go www.xyz.com yyy test"
     
    freerollers, Aug 25, 2008 IP
  2. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #2
    $str = "xyz what go www.xyz.com XyZ test";
    $str = eregi_replace('xyz ', 'yyy ', $str);
    $str = eregi_replace(' xyz', ' yyy', $str);
    echo $str;
     
    grandpa, Aug 25, 2008 IP
  3. freerollers

    freerollers Peon

    Messages:
    185
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that was kinda smart but does that work if theres a line with "xyz" alone?
     
    freerollers, Aug 25, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    The eregi_* functions are old, ugly, slow, and deprecated (they will be removed in PHP 6). Get used to the preg_* functions. And besides that, they're meant for regular expressions, which you're not using. For simple replacements like yours, use str_replace() and str_ireplace().

    Or do it this way:
    
    $text = preg_replace('~(^|\s)xyz($|\s)~si', '$1yyy$2', $text);
    
    PHP:
     
    nico_swd, Aug 25, 2008 IP
  5. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #5
    great solution, but failed on this:
    $str = "xyz xyz xyz www.xyz.com xyz XyZ xyz";
    $str = preg_replace('~(^|\s)xyz($|\s)~si', '$1yyy$2', $str);
    echo $str;
    //output -> yyy xyz yyy www.xyz.com yyy XyZ yyy
    
    Code (markup):
    $str = "xyz xyz xyz www.xyz.com xyz XyZ xyz";
    $str = preg_replace('~(^|\s)xyz($|\s)~si', '$1yyy$2', $str);
    $str = preg_replace('~(^|\s)xyz($|\s)~si', '$1yyy$2', $str);
    echo $str;
    //output -> yyy yyy yyy www.xyz.com yyy yyy yyy
    
    Code (markup):
     
    grandpa, Aug 25, 2008 IP
  6. freerollers

    freerollers Peon

    Messages:
    185
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thx guys :p but writing the same line doesn't look like the perfect solution? must be better way
     
    freerollers, Aug 25, 2008 IP
  7. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    $str = "xyz xyz xyz www.xyz.com xyz XyZ xyz";
    $str = preg_replace('/(^|\s)xyz($|\s)/i', '$1yyy$2', $str);
    echo $str;
    ?>
    Code (php):
    I do think should do what you want. If not, could you expand? :-/
     
    Wrighty, Aug 25, 2008 IP
  8. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #8
    $str = "xyz xyz xyz www.xyz.com xyz XyZ xyz";
    $str = preg_replace(array('/(^|\s)xyz($|\s)/i', '/(^|\s)xyz($|\s)/i'), '$1yyy$2', $str);
    echo $str;
    Code (markup):
     
    grandpa, Aug 25, 2008 IP