Jump to content

Archived

This topic is now archived and is closed to further replies.

cooldude832

Removing non digits in a string

Recommended Posts

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

Share this post


Link to post
Share on other sites
<?php
$var = "1-555-555-5555";
$phone = str_replace("-","","$var");
echo $phone;
?>

Share this post


Link to post
Share on other sites

that is not gonna work because the goal is any non digital value not removing the delimiter (because it could be anything)

Share this post


Link to post
Share on other sites
<?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;
?>

Share this post


Link to post
Share on other sites

*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.

Share this post


Link to post
Share on other sites

*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]/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

*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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

function i use to remove all but numbers:

 

function allNumbers($input_str) {
return preg_replace("/[^0-9]/","",$input_str);
}

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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})?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.