Changing words in text

Discussion in 'PHP' started by wizzard, Aug 9, 2007.

  1. #1
    Hello,

    Is this possibe via php and if possible can you tell me how i can change words in text. Example :

    I have a text where i want to change the word bike in the word car and the word car in the word bike in the same text.

    I know how to change one word and replace it by another but the problem is if i change all the words bike in car then if change car in bike all the words are replaced in bike and thats not what i want.

    I want car -> bike and bike -> car to be changed from the original text.

    Cheers,
    Kris
     
    wizzard, Aug 9, 2007 IP
  2. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #2
    You can try this

    car -> bike1
    bike ->car
    bike1 -> bike
     
    YIAM, Aug 9, 2007 IP
  3. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    If you want to swap words then you will need a temporary third word, something like this:

    
    function SwapWords( $Text, $WordOne, $WordTwo )
    {
    $Text = str_replace( $WordOne, 'TEMPWORD', $Text );
    $Text = str_replace( $WordTwo, $WordOne, $Text );
    $Text = str_replace( 'TEMPWORD', $WordTwo, $Text );
    
    return $Text;
    
    }
    
    PHP:
    Brew
     
    Brewster, Aug 9, 2007 IP
  4. wizzard

    wizzard Peon

    Messages:
    160
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks alot for the great help !
     
    wizzard, Aug 9, 2007 IP