I want to change a string in PHP by deleting the first and the last char but ONLY IF they are equal.
Let me give some examples:
' abc ' should become 'abc'
'abc a' should become 'bc '
' abc a' should not change
How do I do it?
Thanks for the help, the regex based solution works.
You can use the regex:
Regex explanation:
~: Delimiters^: Start anchor(.): match and remember a char (here its the first char)
(.*): match anything and remember\1: recall the first match$: End anchor$2: recall the 2nd matchAlternatively you can do: