swap two words in a string php

前端 未结 6 1201
灰色年华
灰色年华 2020-12-03 14:30

Suppose there\'s a string \"foo boo foo boo\" I want to replace all fooes with boo and booes with foo. Expected output is \"boo foo boo foo\". What I get is \"foo foo foo fo

6条回答
  •  萌比男神i
    2020-12-03 15:07

    First foo to zoo. Then boo to foo and last zoo to boo

    $search = array('foo', 'boo', 'zoo');
    $replace = array('zoo', 'foo', 'boo');
    echo str_replace($search, $replace, $string);
    

提交回复
热议问题