RE: [PHP] Stripping chars in string but leaving numerics
| From: | Jesper Vad Kristensen | Date: | Wed, 02 Aug 2000 08:24:32 +0000 |
| Subject: | RE: [PHP] Stripping chars in string but leaving numerics | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-9646@lists.php.net to get a copy of this message | ||
> 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