What regular expression can I use in PHP to remove all punctuation from the beginning and end of a string?
I wouldn't use a regex, probably something like...
$str = trim($str, '"\'');
Where the second argument is what you define as punctuation.
Assuming what you really meant was to strip out stuff which isn't letters, digits, etc, I'd go with...
$str = preg_replace('/^\PL+|\PL\z/', '', $str);
Might depend on your definition of punctuation. If it's "anything but alphanumerics" or something like that, then a regular expression may be the way to go. But if it's "period, question mark, and exclamation point" or some other manageable list, this will be easier to understand:
trim($string, '?!.');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With