Jump to content

Removing non digits in a string


cooldude832

Recommended Posts

<?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;
?>

Link to comment
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.

Link to comment
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]/

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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})?

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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)

Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.