PHP String Replace function str_replace Definition and Example

php string replace str_replace is a function that help us replace all occurrences of the search string with the replacement string.

The Definition of String Replace function str_replace

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

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

If you do not need fancy replacing rules (like regular expressions), you ought to use this function rather than preg_replace().

The Parameters of String Replace function str_replace

If search and replace are arrays, then str_replace() takes worth from each array and uses them to look and replace on the subject. If replace has fewer values than search, then an empty string is employed for the remainder of replacement values. If search may be an array and replace is a string, then this replacement string is employed for each value of search. The converse wouldn’t add up, though.

If search or replace are arrays, their elements are processed first to last.

searchThe value being looked for otherwise referred to as the needle. An array could also be wont to designate multiple needles.
replaceThe replacement value that replaces found search values. An array could also be wont to designate multiple replacements.
subjectThe string or array being searched and replaced on otherwise referred to as the haystack.
If the subject is an array, then the search and replace is performed with every entry of the subject, and therefore the return value is an array also.
countIf passed, this may be set to the number of replacements performed.

The Return Values of String Replace function str_replace

This function returns a string or an array with the replaced values.

The Example of String Replace function str_replace

        $subject_string = $_POST['subject-string'];// sterilize in production mode
        $search_string = $_POST['search-string'];// sterilize in production mode
        $replace_string = $_POST['replace-string'];// sterilize in production mode

        $return_value = str_replace($search_string,$replace_string,$subject_string);

<html lang="en">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- css -->
    <link href="[email protected]/dist/css/bootstrap.min.css"
    type="text/css" />
    <!-- javascript -->
    <script src="[email protected]/dist/jquery.min.js"></script>
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"></script>

    <div class="container">
        <form method="post" action="index.php">
                PHP str_replace() example
            <div class="form-group">
                <label for="subject-string">Subject String</label>
                <input type="text" class="form-control" id="subject-string" name="subject-string" value="This is a subject string for str_replace.">
            <div class="form-group">
                <label for="search-string">Search String</label>
                <input type="text" class="form-control" id="search-string" name="search-string" value="subject">
                <div class="hint-block">
                    The string you want to search and will be replaced.
            <div class="form-group">
                <label for="replace-string">Replace String</label>
                <input type="text" class="form-control" id="replace-string" name="replace-string" value="title">
                <div class="hint-block">
                    Replace "Search String" in "Subject String" to "Relace String".
            <div class="form-group">
                <label for="return-value">Return Value</label>
                <input type="text" class="form-control" id="return-value" name="return-value" disabled value="<?php echo $return_value; ?>">
            <button type="submit" class="btn btn-primary">Submit</button>


Click submit button and you will see the “Return Value” has been replaced with “This is a title string for str_replace.”.

If you want to test with “Subject Array”, you can do some change with the example code.

  • Change the “Subject String” to something like “Apple Inc, IBM Inc, Microsoft Inc“.
  • Split the string to an array with delimiter “,“.
  • You will get an Array “['Apple Inc','IBM Inc','Microsoft Inc']“.
  • Set “Search String” to “Inc“.
  • Set “Replace String” to “LLC“.
  • You will get the “Return Value”: an array: “['Apple LLC','IBM LLC','Microsoft LLC']“.
  • Use PHP implode() function to convert the “Return Value” to string.

If you have any question, please leave a comment. You can get all the code here “bruce-lab/php-str_replace“.

Leave a Comment

Your email address will not be published. Required fields are marked *