DamienRoche Posted October 10, 2008 Share Posted October 10, 2008 I'm totally confused here. I have this: $string = "numbers = 21,200"; $number = preg_replace("/[^0-9]/", "", $string); This outputs: 21200 But I just don't get it. My understanding is that it is saying - replace all 0-9 with nothing and use string as the subject. does the ^ indicate everything but? Finally, can anyone help me to not filter out the comma? As always, any help or advice is very much appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
redarrow Posted October 10, 2008 Share Posted October 10, 2008 what else you need to no mate.......... <?php $string = "numbers = 21,200"; $number = preg_replace("/[^0-9\,]/", "", $string); echo $number; ?> Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted October 10, 2008 Author Share Posted October 10, 2008 Fantastic...these regex's are numbing my mind. I do have another question. I know you can use \b to indicate an exact word but how do I use it with the ^ anything but I just discovered. Example (won't work): <?php $string = "numbers = 21,200"; $number = preg_replace("/[^0-9\,|\bnumb\b]/", "", $string); echo $number; ?> or: <?php $string = "numbers = 21,200"; $number = preg_replace("/[^0-9\,]|\bnumb\b/", "", $string); echo $number; ?> It just keeps pulling numb from numbers. Thanks again. Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted October 10, 2008 Share Posted October 10, 2008 Finally, can anyone help me to not filter out the comma? As always, any help or advice is very much appreciated. Thanks. if you don't want to filter out the comma, add it inside the range $number = preg_replace("/[^0-9,]/", "", $string); or else, if the string is distinct, just explode it, no need for regexp $a=explode(" ",$string); echo end($a); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 10, 2008 Share Posted October 10, 2008 <?php $string = "numbers = 21,200"; $number = preg_replace("/[^0-9\,]|\bnumb\b/", "", $string); echo $number; ?> This pattern is not logical. You are telling regex to replace anything that is NOT 0-9 or a comma (you don't need to escape the comma), -or- the word num with nothingness (""). Since this is an alternation, the first condition is met, thus everything that is not a number nor a comma will be wiped out. In other words, if there is a comma or a number in your string, this is the ONLY condition that will ever be met. As for your \bnum\b part, this will never work when checked against 'number'. Text nested within the \b...\b characters are surrounded by non-word characters... you cannot use \b...\b to find a part of a word. It matches a complete word. So in this case, your pattern is searching for a word by itself spelled num. My strong advice is to read up on regex here to get a better understanding. Constantly 'shooting in the dark' is not the best way to learn. Read up on regex material, then experiment. 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.