Re: Stripping chars in string but leaving numerics
| From: | Steve Edberg | Date: | Wed, 02 Aug 2000 09:12:05 +0000 |
| Subject: | Re: Stripping chars in string but leaving numerics | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9654@lists.php.net to get a copy of this message | ||
At 10:35 AM +0200 8/2/00, Anders Dannberg wrote:
I managed to get it to work with ereg_replace ($tesstr = ereg_replace ("[a-z, A-Z, / etc etc...]", "", $string); However, this solution seems alot smoother. Thanks for the help! AndersActually, to be even cleaner: $CleanedString = ereg_replace('[^[:digit:]]', '' $string); will remove everything that's not a digit. - steve edberg
+--- "They've got a cherry pie there, that'll kill ya" ------------------+So if i have a string with value "abcd*()123" i want the output to be "123"In the case above I'd use a regular expression to match this. For that task you'll need the /d instruction which matches digits. In your example matching the 123 would be easy: preg_match("/(\d+)/i", "abcd*()123", $matches) $matches will be an array, and $matches[0] contains the whole string, and $matches[1] contains the first match from a pattern in paranthesis, i.e. the $matches[1] = "123" What happens? /d+ matches a digit one or more times, that's what the plus sign means. preg_match is a Perl Regular Expression match found in PHP 3.0.9 and ahead. If you'd had a string like "321abcd*()123" and wanted the "321123" out you might have to loop over the preg_match. --Jesper
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+