PHP String Exercises: Remove trailing slash from a string
Write a PHP script to remove trailing slash from a string.
Original String : 'The quick brown fox jumps over the lazy dog///'
Visual Presentation:
Sample Solution:
PHP Code:
<?php
$my_str = 'The quick brown fox jumps over the lazy dog///';
// Define the original string.
echo rtrim($my_str, '/')."\n";
// Remove trailing slashes from the original string and output the result.
?>
Sample Output:
The quick brown fox jumps over the lazy dog
Explanation:
The above PHP code snippet takes the string 'The quick brown fox jumps over the lazy dog///' and removes any trailing slashes from it using the "rtrim()" function. The "rtrim()" function trims characters from the right side of a string. Here, it trims trailing slashes from the string, resulting in the string 'The quick brown fox jumps over the lazy dog'. Finally, it echoes out the modified string.
Flowchart :
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to remove part of a string.
Next: Write a PHP script to get the characters after the last '/' in an url.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics