cooldude832 Posted August 28, 2008 Share Posted August 28, 2008 I've tried to do with regex but is there a non regex way any one can think of to replace non digit values in a string with nothing like 1-555-555-5555 becomes 15555555555 Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/ Share on other sites More sharing options...
dezkit Posted August 28, 2008 Share Posted August 28, 2008 <?php $var = "1-555-555-5555"; $phone = str_replace("-","","$var"); echo $phone; ?> Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627525 Share on other sites More sharing options...
cooldude832 Posted August 28, 2008 Author Share Posted August 28, 2008 that is not gonna work because the goal is any non digital value not removing the delimiter (because it could be anything) Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627527 Share on other sites More sharing options...
Ken2k7 Posted August 28, 2008 Share Posted August 28, 2008 <?php // the given string $text = "adsf21asf2123asdfasf"; // number selection $selection = "0123456789"; $arr = str_split($text); $len = count($arr); $count = -1; $output = ""; while (++$count < $len) { $selected = $arr[$count]; if (strpos($selection, $selected) !== false) $output .= $selected; } echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627549 Share on other sites More sharing options...
cooldude832 Posted August 28, 2008 Author Share Posted August 28, 2008 now that is fancy ! Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627566 Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 *cough cough* Doesn't anyone know the amazing power of... Regular Expressions [imagine Morgan Freeman] <?php // the given string $text = "adsf21asf2123asdfasf"; $pattern = '/[^0-9]*/'; echo preg_replace($pattern,'', $string); ?> Now that's short and simple! RegEx is probably amazingly efficient. There are a lot of tutorials out about this kind of stuff... But basically, here's how it works in the above example: [0-9] means any digit. [^0-9] means any NON-digit. [^0-9]* means at least zero of any NON-digit. So it finds any non-digit, no matter how many non-digits in a row it finds, and removes it. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627574 Share on other sites More sharing options...
Ken2k7 Posted August 28, 2008 Share Posted August 28, 2008 *cough cough* Doesn't anyone know the amazing power of... Regular Expressions [imagine Morgan Freeman] <?php // the given string $text = "adsf21asf2123asdfasf"; $pattern = '/[^0-9]*/'; echo preg_replace($pattern,'', $string); ?> Now that's short and simple! RegEx is probably amazingly efficient. There are a lot of tutorials out about this kind of stuff... But basically, here's how it works in the above example: [0-9] means any digit. [^0-9] means any NON-digit. [^0-9]* means at least zero of any NON-digit. So it finds any non-digit, no matter how many non-digits in a row it finds, and removes it. Too bad the OP asks for a non-regex way. A better regex is: /[^\d]/ Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627580 Share on other sites More sharing options...
Psycho Posted August 28, 2008 Share Posted August 28, 2008 Too bad the OP asks for a non-regex way. True, but the OP stated I've tried to do with regex but... Which would lead me to belive he was unsuccessful in doing so. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627584 Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 *cough cough* Doesn't anyone know the amazing power of... Regular Expressions [imagine Morgan Freeman] <?php // the given string $text = "adsf21asf2123asdfasf"; $pattern = '/[^0-9]*/'; echo preg_replace($pattern,'', $string); ?> Now that's short and simple! RegEx is probably amazingly efficient. There are a lot of tutorials out about this kind of stuff... But basically, here's how it works in the above example: [0-9] means any digit. [^0-9] means any NON-digit. [^0-9]* means at least zero of any NON-digit. So it finds any non-digit, no matter how many non-digits in a row it finds, and removes it. Too bad the OP asks for a non-regex way. A better regex is: /[^\d]/ Yes, the Character Class always helps.. But perhaps it's much easier with... /[\D]/ Ehh? (for some reason, character classes don't seem to work on my server, but I still have plenty other ways to write a character class out, IE: [^0-9]) Too bad the OP asks for a non-regex way. True, but the OP stated I've tried to do with regex but... Which would lead me to belive he was unsuccessful in doing so. Yes, I thought of this too. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627585 Share on other sites More sharing options...
Ken2k7 Posted August 28, 2008 Share Posted August 28, 2008 mjdamato: Nice point. I think people rely on Regex more than they should. Of course anything that doesn't require regex, don't use it. I do believe it is way more intensive. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627601 Share on other sites More sharing options...
libertyct Posted August 28, 2008 Share Posted August 28, 2008 simply cast the variable to an integer type e.g. (int)$variableName; Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627806 Share on other sites More sharing options...
BlueSkyIS Posted August 28, 2008 Share Posted August 28, 2008 function i use to remove all but numbers: function allNumbers($input_str) { return preg_replace("/[^0-9]/","",$input_str); } Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627812 Share on other sites More sharing options...
cooldude832 Posted August 28, 2008 Author Share Posted August 28, 2008 I wasn't unsuccessful with regex I was looking to challenge people. I don't like preg I like ereg or eregi so my regex solution was <?php $string= "555-555-5555"; $pattern = "[^0-9]"; echo eregi_replace($pattern,"",$string); ?> I know how to do regex Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627877 Share on other sites More sharing options...
DarkWater Posted August 28, 2008 Share Posted August 28, 2008 cooldude, although you may not like PCRE, you really shouldn't use POSIX Extended at all any more because it's been announced that it's completely removed from PHP6 and will ONLY be available with an extension (something most hosting companies won't do). You should always program for forward-compatibility. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627882 Share on other sites More sharing options...
discomatt Posted August 28, 2008 Share Posted August 28, 2008 Regex is slow... especially for tasks as simple as this. <?php $str = '1234sargra567aery890'; $num = ''; for( $i=0,$c=strlen($str); $i<$c; $i++ ) if ( is_numeric($str[$i]) ) $num .= $str[$i]; echo $num; # Outputs '1234567890' ?> In theory, it'll run about 10-40x faster than even a simple regex. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627885 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 Regex is slow... especially for tasks as simple as this. <?php $str = '1234sargra567aery890'; $num = ''; for( $i=0,$c=strlen($str); $i<$c; $i++ ) if ( is_numeric($str[$i]) ) $num .= $str[$i]; echo $num; # Outputs '1234567890' ?> In theory, it'll run about 10-40x faster than even a simple regex. correct me if i'm wrong, but if you're specifying a character within a string, don't you use braces rather than brackets (ie. $str{$i})? Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627888 Share on other sites More sharing options...
cooldude832 Posted August 28, 2008 Author Share Posted August 28, 2008 regex really isn't that slow, Where is this documentation on POSIX leaving php 6? Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627889 Share on other sites More sharing options...
DarkWater Posted August 28, 2008 Share Posted August 28, 2008 I'll get it for you in a sec, cooldude. @akitchin: Yeah, until PHP6 where you can use either, but [] gives you slice capabilities. >_< It's { } for now. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627891 Share on other sites More sharing options...
DarkWater Posted August 28, 2008 Share Posted August 28, 2008 Meeting minutes discussing PHP6 changes Pretty good read actually. Anyway, if you don't feel like clicking the link: 3.3 Move ereg to PECL Issue: Currently we have two extensions dealing with regular expressions, and soon there will be a third one based on ICU. Discussion: Currently we see some problems with the bundled ereg library in some places due to people specifying --with-regex=system. We also see distributions linking against another library than our bundled one to prevent conflicts with the apache bundled regex library, or the system's one. As most people seem to prefer linking against something else than our bundled version, it seems proper to remove this bundled library. If we remove the bundled library, then we need to make the ereg functions into an extension, otherwise we can not enable them in all cases. Some functionality in the core of PHP also uses POSIX regular expressions, those should be rewritten to use PCRE then. Conclusions: 1. We make ereg an extension 2. The PCRE extension will not be allowed to be disabled. 3. The core of PHP should be made to work with PCRE so that we can safely disable ereg 4. We unbundle the regex library Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627894 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 I'll get it for you in a sec, cooldude. @akitchin: Yeah, until PHP6 where you can use either, but [] gives you slice capabilities. >_< It's { } for now. just looked at the manual. apparently we've always been able to use [], and {} will be deprecated as of PHP 6. you learn something new everyday. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627897 Share on other sites More sharing options...
DarkWater Posted August 28, 2008 Share Posted August 28, 2008 I'll get it for you in a sec, cooldude. @akitchin: Yeah, until PHP6 where you can use either, but [] gives you slice capabilities. >_< It's { } for now. just looked at the manual. apparently we've always been able to use [], and {} will be deprecated as of PHP 6. you learn something new everyday. Oh, really? I knew that { } would be deprecated in PHP6, but I was a bit unsure on the status of [ ]. Cools. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627898 Share on other sites More sharing options...
discomatt Posted August 28, 2008 Share Posted August 28, 2008 correct me if i'm wrong, but if you're specifying a character within a string, don't you use braces rather than brackets (ie. $str{$i})? Both will work, I'm not sure exactly when it was implemented, but I've been able to do it in PHP 5.2.x regex really isn't that slow, O Rly? My statement wasn't just a random knock on regex. Test it. PHP's basic string manipulation functions are leaps and bounds faster. I'm not saying don't ever use regex, but if you want to code efficiently you should limit its use to only very complex string manipulation. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627902 Share on other sites More sharing options...
cooldude832 Posted August 28, 2008 Author Share Posted August 28, 2008 The problem with your example is there is no apples to apple comparison in both cases as the string length increases the speed of processing goes down. However in your case the string length increase is a much more dramatic increase in time than in regex so you could say 2 lines of y = mx+b where y = process time x = string length for regex it be like y = 5(x) + 15 and for you version y = 7.5(x) + 5 So eventually they cross and regex would be faster (I don't know the exact numbers just showing example wise) Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627915 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 The problem with your example is there is no apples to apple comparison in both cases as the string length increases the speed of processing goes down. However in your case the string length increase is a much more dramatic increase in time than in regex so you could say 2 lines of y = mx+b where y = process time x = string length for regex it be like y = 5(x) + 15 and for you version y = 7.5(x) + 5 So eventually they cross and regex would be faster (I don't know the exact numbers just showing example wise) care to offer a source on that? REGEX forces PHP to analyse the string entirely to find matches, whereas the string method requires only that PHP processes the string bit by bit. both methods increase in processing time with string length, i would argue at about the same rate each. Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-627917 Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 This is gonna be a neverending argument at this point. Flat and simple: if you're not dealing with strings that are on the order of 5million characters or something, there shouldn't be THAT much of a difference in speed (I mean like within 5 seconds of each other honestly...). My experience with RegEx has been fantastic, it works just the way I want it to, same with string manipulation. If you want it to work, pick either one. If you want it to work fast, do some research :-P Quote Link to comment https://forums.phpfreaks.com/topic/121643-removing-non-digits-in-a-string/#findComment-628039 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.