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

<?php
    if(!empty($_POST)){
        $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">

<head>
    <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="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
    type="text/css" />
    
    <!-- javascript -->
    <script src="https://cdn.jsdelivr.net/gh/jquery/[email protected]/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    

</head>
<body>
    <div class="container">
        <form method="post" action="index.php">
            <h1>
                PHP str_replace() example
            </h1>
            <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>
            <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>
            </div>
            <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>
            </div>
            <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; ?>">
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>
</body>

</html>

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 *