John_A Posted January 11, 2012 Share Posted January 11, 2012 I've got a string which is usually abc-def-0123 but it sometimes has an additional dash and then a single digit (e.g. ghi-jkl-4567-1). It's always the same pattern and length, (three letters, a dash, three letters, a dash, 4 digits then the possibility of a dash followed by a single digit). What's the best way to test if the string ends in a dash followed by a single digit? Any help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/ Share on other sites More sharing options...
scootstah Posted January 11, 2012 Share Posted January 11, 2012 Regular expressions. $str = 'ghi-jkl-4567-1'; if (preg_match('/-[0-9]{1}$/', $str)) { echo 'match'; } Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306524 Share on other sites More sharing options...
John_A Posted January 11, 2012 Author Share Posted January 11, 2012 Many thanks! What's the best way to then remove the offending -[digit] from the string (sorry, I should have included this in my original question)? Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306526 Share on other sites More sharing options...
scootstah Posted January 11, 2012 Share Posted January 11, 2012 Still regular expressions. $str = 'ghi-jkl-4567-1'; preg_replace('/-[0-9]{1}$/', '', $str); Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306527 Share on other sites More sharing options...
John_A Posted January 11, 2012 Author Share Posted January 11, 2012 Excellent, just what I needed. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306533 Share on other sites More sharing options...
Psycho Posted January 11, 2012 Share Posted January 11, 2012 Still regular expressions. $str = 'ghi-jkl-4567-1'; preg_replace('/-[0-9]{1}$/', '', $str); Why the {1}? You already have the dollar sign to indicate only the end of the string so no repetition criteria is necessary. @John_A: It may not be clear, but with scootstah's replacement solution you do not need to test if there is a -[digit] before trying to remove it. Just run the preg_replace() to remove the -[digit]. If it does not exist it won't be replaced. So, testing for it is a waste of code. For what it's worth, I would use this expression: $str = preg_replace('/-\d$/', '', $str); The \d is a character class for digit. Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306537 Share on other sites More sharing options...
John_A Posted January 11, 2012 Author Share Posted January 11, 2012 Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306547 Share on other sites More sharing options...
scootstah Posted January 11, 2012 Share Posted January 11, 2012 Still regular expressions. $str = 'ghi-jkl-4567-1'; preg_replace('/-[0-9]{1}$/', '', $str); Why the {1}? You already have the dollar sign to indicate only the end of the string so no repetition criteria is necessary. @John_A: It may not be clear, but with scootstah's replacement solution you do not need to test if there is a -[digit] before trying to remove it. Just run the preg_replace() to remove the -[digit]. If it does not exist it won't be replaced. So, testing for it is a waste of code. For what it's worth, I would use this expression: $str = preg_replace('/-\d$/', '', $str); The \d is a character class for digit. My regex is mediocre at best. Thanks for the optimization. Quote Link to comment https://forums.phpfreaks.com/topic/254807-test-if-string-ends-in-number/#findComment-1306548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.