I would like to take a string, and strip any characters apart from 0-9 and - (dashes).
Example:
if I have a string that looks like:
10-abc20-30
How can I make this string return
10-20-30
(Strip all characters besides numbers and dashes)
Is there some kind of regex to use within preg_match or str_replace ?
$result = preg_replace('/[^\d-]+/', '', $subject);
[^\d-]
matches any character except digits or dash; the +
says "one or more" of those, so adjacent characters will be replaced at once.
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