Skip to content

Instantly share code, notes, and snippets.

@melvinstanly
Created October 4, 2021 09:24
Show Gist options
  • Save melvinstanly/0056d780eaedb0b3d4a7905f8a1d8f94 to your computer and use it in GitHub Desktop.
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
<?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