PHP Program to replace a word in a string using search and replace

bookmark

<?php
    // string
    $str = 'We welcome all of you to our school of PHP.
                This school is one of its kind.
                Many schools dont offer this subject.';
    $s = 'school';
    $newstr = 'college';
    
    // printing message
    echo ('Old message-->' . $str);
    echo ("\n");
    echo ('New message-->');
    
    // Using preg_replace() function
    // to replace the word
    $str = preg_replace('/school/', $newstr, $str);
    
    // Printing the result
    echo $str;
?>


Output

Old message-->We welcome all of you to our school of PHP.
                This school is one of its kind.
                Many schools dont offer this subject.
New message-->We welcome all of you to our college of PHP.
                This college is one of its kind.
                Many colleges dont offer this subject.