Created
October 4, 2021 09:24
-
-
Save melvinstanly/0056d780eaedb0b3d4a7905f8a1d8f94 to your computer and use it in GitHub Desktop.
Remove a part of a string, but only when it is at the end of the string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cleaned = preg_replace('/'. preg_quote($remove, '/') . '$/', '', $cleaned); | |
//$remove -> The string to be removed which is input by the user | |
// Or you can use a regular expression as shown below | |
$cleaned = preg_replace('/\[\]$/', '', $cleaned); // => Removes square brackets at the end of the string. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment