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:

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:

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?
Test your Programming skills with w3resource's quiz.
PHP: Tips of the Day
$_COOKIE: This SuperGlobal Variable is used to retrieve Cookie value with given Key.
Example:
<?php $cookie_name = "data"; $cookie_value = "Foo Bar"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!"; echo "\n"; echo "Value is: " . $_COOKIE[$cookie_value]; } ?>
Output:
Cookie named 'data' is not set!
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework