w3resource
php Programming Exercies

PHP String Exercises: Replace the first word with another word

PHP String: Exercise-10 with Solution

Write a PHP script to replace the first 'the' of the following string with 'That'.

Sample date: 'the quick brown fox jumps over the lazy dog.'

Pictorial Presentation:

PHP String Exercises: Replace the first word with another word

Sample Solution:

PHP Code:

<?php
$str = 'the quick brown fox jumps over the lazy dog.';
echo preg_replace('/the/', 'That', $str, 1)."\n"; 
?>

Sample Output:

That quick brown fox jumps over the lazy dog.

Flowchart:

Flowchart: Replace the first word with another word

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to generate simple random password [do not use rand() function] from a given string.
Next: Write a PHP script to find the first character that is different between two strings.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming