PHP String - Exercises, Practice, Solution
PHP string [26 exercises with solution]
1. Write a PHP script to : - Go to the editor
a) transform a string all uppercase letters.
b) transform a string all lowercase letters.
c) make a string's first character uppercase.
d) make a string's first character of all the words uppercase.
Click me to see the solution
2. Write a PHP script to split the following string. Go to the editor
Sample string : '082307'
Expected Output : 08:23:07
Click me to see the solution
3. Write a PHP script to check whether a string contains a specific string? Go to the editor
Sample string : 'The quick brown fox jumps over the lazy dog.'
Check whether the said string contains the string 'jumps'.
Click me to see the solution
4. Write a PHP script to convert the value of a PHP variable to string. Go to the editor
Click me to see the solution
5. Write a PHP script to extract the file name from the following string. Go to the editor
Sample String : 'www.example.com/public_html/index.php'
Expected Output : 'index.php'
Click me to see the solution
6. Write a PHP script to extract the user name from the following email ID. Go to the editor
Sample String : '[email protected]'
Expected Output : 'rayy'
Click me to see the solution
7. Write a PHP script to get the last three characters of a string. Go to the editor
Sample String : '[email protected]'
Expected Output : 'com'
Click me to see the solution
8. Write a PHP script to format values in currency style. Go to the editor
Sample values : value1 = 65.45, value2 = 104.35
Expected Result : 169.80
Click me to see the solution
9. Write a PHP script to generate simple random password [do not use rand() function] from a given string. Go to the editor
Sample string : '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz'
Note : Password length may be 6, 7, 8 etc.
Click me to see the solution
10. Write a PHP script to replace the first 'the' of the following string with 'That'. Go to the editor
Sample date : 'the quick brown fox jumps over the lazy dog.'
Expected Result : That quick brown fox jumps over the lazy dog.
Click me to see the solution
11. Write a PHP script to find the first character that is different between two strings. Go to the editor
String1 : 'football'
String2 : 'footboll'
Expected Result : First difference between two strings at position 5: "a" vs "o"
Click me to see the solution
12. Write a PHP script to put a string in an array. Go to the editor
Sample strings : "Twinkle, twinkle, little star,\nHow I wonder what you are.\nUp above the world so high,\nLike a diamond in the sky.";
Expected Result (using var_dump()) : array(4) { [0]=> string(30) "Twinkle, twinkle, little star," [1]=> string(26) "How I wonder what you are." [2]=> string(27) "Up above the world so high," [3]=> string(26) "Like a diamond in the sky." }
Click me to see the solution
13. Write a PHP script to get the filename component of the following path. Go to the editor
Sample path : "https://www.w3resource.com/index.php"
Expected Output : 'index'
Click me to see the solution
14. Write a PHP script to print the next character of a specific character. Go to the editor
Sample character : 'a'
Expected Output : 'b'
Sample character : 'z'
Expected Output : 'a'
Click me the solution
15. Write a PHP script to remove a part of a string from the beginning. Go to the editor
Sample string : '[email protected]'
Expected Output : 'example.com'
Click me to see the solution
16. Write a PHP script to get a hex dump of a string. Go to the editor
Sample string : '[email protected]'
Click me to see the solution
17. Write a PHP script to insert a string at the specified position in a given string. Go to the editor
Original String : 'The brown fox'
Insert 'quick' between 'The' and 'brown'.
Expected Output : 'The quick brown fox'
Click me to the see solution
18. Write a PHP script to get the first word of a sentence. Go to the editor
Original String : 'The quick brown fox'
Expected Output : 'The'
Click me to see the solution
19. Write a PHP script to remove all leading zeroes from a string. Go to the editor
Original String : '000547023.24'
Expected Output : '547023.24'
Click me to see the solution
20. Write a PHP script to remove part of a string. Go to the editor
Original String : 'The quick brown fox jumps over the lazy dog'
Remove 'fox' from the above string.
Expected Output : 'The quick brown jumps over the lazy dog'
Click me to see the solution
21. Write a PHP script to remove trailing slash from a string. Go to the editor
Original String : 'The quick brown fox jumps over the lazy dog///'
Expected Output : 'The quick brown fox jumps over the lazy dog'
Click me to see the solution
22. Write a PHP script to get the characters after the last '/' in an url. Go to the editor
Sample URL : 'https://www.example.com/5478631'
Expected Output : '5478631'
Click me to see the solution
23. Write a PHP script to replace multiple characters from the following string. Go to the editor
Sample String : '\"\1+2/3*2:2-3/4*3'
Expected Output : '1 2 3 2 2 3 4 3'
Click me to see the solution
24. Write a PHP script to select first 5 words from the following string. Go to the editor
Sample String : 'The quick brown fox jumps over the lazy dog'
Expected Output : 'The quick brown fox jumps'
Click me to see the solution
25. Write a PHP script to remove comma(s) from the following numeric string. Go to the editor
Sample String : '2,543.12'
Expected Output : 2543.12
Click me to see the solution
26. Write a PHP script to print letters from 'a' to 'z'. Go to the editor
Expected Result : abcdefghijklmnopqrstuvwxyz
Click me to see the solution
PHP Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- 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