How to replace all occurrences of two substrings with str_replace()?

前端 未结 3 699
-上瘾入骨i
-上瘾入骨i 2021-01-28 13:20

Currently I have this code which replaces any double space with a
.

It works as expected:



        
3条回答
  •  死守一世寂寞
    2021-01-28 14:11

    The order of the array does matter otherwise you will get instead of
    so try:

    str_replace(array(' ','||'), array('|','

    '), trim($result['garment_type'] ));

    Something like this:

    echo str_replace(array(' ','||'), array('|','

    '), 'crunchy bugs are so tasty man');

    Gives you:

    crunchy

    bugs|are|so

    |tasty|man

    Basically you are changing each space first to | then you are changing any that have two beside each other (||) to

    .

    If you go the other way, you will change two spaces to

    and then you are changing single spaces to | and inbetween the
    there is a space, so you end up with .

    EDIT with your code:

    '
        Garments:
        ' . str_replace(array(' ','||'), array('|','

    '), trim($result['garment_type'] )) . ' '

提交回复
热议问题