kts Posted January 23, 2009 Share Posted January 23, 2009 Hi, I'm trying to cut a string. I have anywhere from 1 to 10 #s then " - " then item name. I am trying to just cut the number from the beginning. I was using the trim functions and such, but it is a very bad practice. I was told you can accomplish this mostly error free in regex? I tried [0-9], but it only grabbed one digit. Quote Link to comment Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 [0-9] will match any single integer [0-9]{10} will match any 10 integers ^[0-9]{10} will match any 10 integers fron the start Quote Link to comment Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 Take a look at Nick's Regular Expressions tutorial Quote Link to comment Share on other sites More sharing options...
.josh Posted January 23, 2009 Share Posted January 23, 2009 you need to give some actual example formats. Are there spaces between the hyphen? anything else on the line? etc.. Quote Link to comment Share on other sites More sharing options...
kts Posted January 23, 2009 Author Share Posted January 23, 2009 it has space after the id and then a hyphen as well as another space after. from testing it seems if I grab the numbers from start /^[0-9]{1,}/ seems to be working fine, but someone mentioned /d+? Quote Link to comment Share on other sites More sharing options...
.josh Posted January 23, 2009 Share Posted January 23, 2009 ^\d+ Quote Link to comment Share on other sites More sharing options...
.josh Posted January 23, 2009 Share Posted January 23, 2009 fyi {1,} is the same as doing + and \d is mostly the same as doing [0-9] (it also includes exponents) Quote Link to comment Share on other sites More sharing options...
kts Posted January 23, 2009 Author Share Posted January 23, 2009 Cool thanks a lot guys. Regex is much more powerful than I thoguht. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted January 23, 2009 Share Posted January 23, 2009 $str = 'Cool thanks a lot guys. Regex is much more powerful than I thoguht.'; $str = str_replace('ogu', 'oug', $str); Quote Link to comment 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.