How do I replace certain parts of my string?

后端 未结 5 925
孤独总比滥情好
孤独总比滥情好 2020-11-22 04:29

How can I replace a certain part of my string with another one?

Input string:

\"Hello, my name is Santa\"

How can I change all

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 05:03

    Search & Replace

    There are a few different functions/methods to replace a certain part of a string with something else, all with their own advantages.


    ##str_replace() method (binary safe; case-sensitive)

    Arguments

    mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

    str_replace() has 3 required arguments as you can see in the above definition with the correct order, all of which can take a string as also an array as argument!

    Search & Replace

    • search(string) AND replace(string) → Replaces the search string with the replace string.

    • search(array) AND replace(string) → Replaces all search elements with the replace string.

    • search(string) AND replace(array) → Throws you a notice: "Notice: Array to string conversion", because a replacement array for just one search string doesn't make sense, so it tries to convert the array to a string.

    • search(array) AND replace(array) → Replaces each search element with the corresponding replace element (Keys are ignored!).

    • search(more elements) AND replace(less elements) → Replaces each search element with the corresponding replace element (For the missing replace elements an empty string will be used).

    • search(less elements) AND replace(more elements) → Replaces each search element with the corresponding replace element (Unneeded replace elements are ignored).

    Subject

    • subject(string) → Replacement is done for the subject string.

    • subject(array) → Replacement is done for each array element.

    Code

    echo str_replace("search", "replace", "text search text");
    echo str_replace(["t", "a"], "X", "text search text");
    echo str_replace("search", ["replace1", "replace2"], "text search text");
    echo str_replace(["a", "c"], ["X", "Y"], "text search text");
    

    Output

    text replace text
    XexX seXrch XexX
    Notice: Array to string conversion
    text seXrYh text
    

    Notes

    1. Gotcha!

    Important to know is that str_replace() works from left to right of the array. This means it can possible replace a value which you already replaced. For example:

        echo str_replace(array("a", "b"), array("b", "c"), "aabb");
        //Probably expected output: bbcc
        //Actual output:            cccc
    
    1. Case insensitive

    If you want to make the search case insensitive you can use str_ireplace() (Notice the i for case-insensitive).

    1. Multidimensional array

    str_replace()/str_ireplace() does NOT work for multidimensional arrays. See this manual comment for such an implementation. Of course you can also replace str_replace() with str_ireplace() for case-insensitive.

    If you want to put everything together and create a function that also works for multidimensional arrays case-insensitive you can do something like this:


    ##strtr() method (50% binary safe; case-sensitive)

    Arguments

    string strtr ( string $str , string $from , string $to )

    string strtr ( string $str , array $replace_pairs )

    The function either takes 3 arguments with a from and to string or it takes 2 arguments with a replacement array array("search" => "replace" /* , ... */), all of which you can see in the above definition with the correct order.

    2 Arguments

    It starts to replace the longest key with the corresponding value and does this until it replaced all key => value pairs. In this case the function is binary safe, since it uses the entire key/value.

    3 Arguments

    It replaces the from argument with the to argument in the subject byte by byte. So it is not binary safe!

    If the from and to arguments are of unequal length the replacement will stop when it reaches the end of the shorter string.

    Subject

    It doesn't accept an array as subject, just a string.

    Code

    echo strtr("text search text", "ax", "XY");;
    echo strtr("text search text", ["search" => "replace"]);
    

    Output

    teYt seXrch teYt
    text replace text
    

    Notes

    1. Gotcha!

    Opposed to str_replace(), strtr() does NOT replace replaced strings. As an example:

        echo strtr("aabb", ["a" => "b", "b" => "c"]);
        //If expecting to replace replacements: cccc
        //Actual output:                        bbcc
    

    Also if you want to replace multiple things with the same string you can use array_fill_keys() to fill up your replacement array with the value.

    1. Case insensitive

    strtr() is NOT case-insensitive NOR is there a case-insensitive equivalent function. See this manual comment for a case-insensitive implementation.

    1. Multidimensional array

    strtr() does opposed to str_replace() NOT work with arrays as subject, so it also does NOT work with multidimensional arrays. You can of course use the code above from str_replace() for multidimensional arrays and just use it with strtr() or the implementation of stritr().

    If you want to put everything together and create a function that also works for multidimensional arrays case-insensitive you can do something like this:

     0  ){
                $positions = array();
                foreach(  $one as $from => $to  ){
                    if(   (  $pos2 = stripos( $product, $from, $pos1 )  ) === FALSE   ){
                        unset(  $one[ $from ]  );
                    }
                    else{
                        $positions[ $from ] = $pos2;
                    }
                }
                if(  count( $one ) <= 0  )break;
                $winner = min( $positions );
                $key = array_search(  $winner, $positions  );
                $product = (   substr(  $product, 0, $winner  ) . $one[$key] . substr(  $product, ( $winner + strlen($key) )  )   );
                $pos1 = (  $winner + strlen( $one[$key] )  );
            }
            return $product;
        }
        else{
            return $string;
        }
    }/* endfunction stritr */
    }/* endfunction exists stritr */
    
    function stritr_deep($string, $one = NULL, $two = NULL){
    if (is_array($string)) 
    { 
        foreach($string as &$oneSubject) 
            $oneSubject = stritr($string, $one, $two); 
        unset($oneSubject); 
        return $string; 
    } else { 
        return stritr($string, $one, $two); 
    } 
    
    }
    ?>


    ##preg_replace() method (binary safe; case-sensitive)

    Arguments

    mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

    preg_replace() has 3 required parameters in the order shown above. Now all 3 of them can take a string as also an array as argument!

    Search & Replace

    • search(string) AND replace(string) → Replaces all matches of the search regex with the replace string.

    • search(array) AND replace(string) → Replaces all matches of each search regex with the replace string.

    • search(string) AND replace(array) → Throws you a warning: "Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array", because a replacement array for just one search regex doesn't make sense.

    • search(array) AND replace(array) → Replaces all matches of each search regex with the corresponding replace element(Keys are ignored!).

    • search(more elements) AND replace(less elements) → Replaces all matches of each search regex with the corresponding replace element(For the missing replace elements an empty string will be used).

    • search(less elements) AND replace(more elements) → Replaces all matches of each search regex with the corresponding replace element(Unneeded replace elements are ignored).

    Subject

    • subject(string) → Replacement is done for the subject string.

    • subject(array) → Replacement is done for each array element.

    Please note again: The search must be a regular expression! This means it needs delimiters and special characters need to be escaped.

    Code

    echo preg_replace("/search/", "replace", "text search text");
    echo preg_replace(["/t/", "/a/"], "X", "text search text");
    echo preg_replace("/search/", ["replace1", "replace2"], "text search text");
    echo preg_replace(["a", "c"], ["X", "Y"], "text search text");
    

    Output

    text replace text
    XexX seXrch XexX
    Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
    text seXrYh text
    

    Notes

    1. Gotcha!

    Same as str_replace(), preg_replace() works from left to right of the array. This means it can possible replace a value which you already replaced. For example:

        echo preg_replace(["/a/", "/b/"], ["b", "c"], "aabb");
        //Probably expected output: bbcc
        //Actual output:            cccc
    
    1. Case insensitive

    Since the search argument is a regular expression you can simply pass the flag i for case-insensitive search.

    1. Multidimensional array

    preg_replace() does NOT work for multidimensional arrays.

    1. Backreference

    Be aware that you can use \\n/$n as backreference to your capturing groups of the regex. Where 0 is the entire match and 1-99 for your capturing groups.

    Also if the backreference is immediately followed by a number you have to use \${n}.

    1. Replacement / "The /e modifier is deprecated"

    The replacement in preg_replace() can't use callback functions as replacements. So you have to use preg_replace_callback(). Same when you use the modifier e and get "Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead". See: Replace preg_replace() e modifier with preg_replace_callback

    If you want to put everything together and create a function that also works for multidimensional arrays case-insensitive you can do something like this:


    ##Loops while / for / foreach method (NOT binary safe; case-sensitive)

    Now of course besides all of those functions you can also use a simple loop to loop through the string and replace each search => replace pair which you have.

    But this gets way more complex when you do it binary safe, case-insensitive and for multidimensional arrays than just using the functions above. So I won't include any examples here.



    Affected String

    Right now all methods shown above do the replacement over the entire string. But sometimes you want to replace something only for a certain part of your string.

    For this you probably want to/can use substr_replace(). Or another common method is to use substr() and apply the replacement only on that particular substring and put the string together afterwards. Of course you could also modify your regex or do something else to not apply the replacement to the entire string.

提交回复
热议问题