Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help with preg_replace

3 views
Skip to first unread message

bill

unread,
Apr 13, 2017, 7:58:06 AM4/13/17
to
I am trying to strip all non-numeric characters from a string.
This is my best guess, but it doesn't do anything to the input
string.

$number =preg_replace("[^0-9]","",$phone);

when $phone is '1a2bc34567890'

--bill

Tony Mountifield

unread,
Apr 13, 2017, 8:06:33 AM4/13/17
to
In article <ocnp48$1oei$1...@gioia.aioe.org>,
The pattern string needs to include delimiters within the string,
so in your case, something like "/[^0-9]/".

You can also use the specifier \D to mean non-digit:

$phone = '1a2bc34567890';
$number = preg_replace("/\D/","",$phone);
echo "$phone => $number";

Produces:
1a2bc34567890 => 1234567890

Cheers
Tony

--
Tony Mountifield
Work: to...@softins.co.uk - http://www.softins.co.uk
Play: to...@mountifield.org - http://tony.mountifield.org

Thomas 'PointedEars' Lahn

unread,
Apr 13, 2017, 8:51:50 AM4/13/17
to
Tony Mountifield wrote:

> $number = preg_replace("/\D/","",$phone);

This should be

$number = preg_replace('/\D/', '', $phone);

Although it does not make a difference here, in general I recommend to
consistently write string literals where no variable expansion is intended
and where there are no straight apostrophes in the value, in single-quotes
(straight apostrophes) in PHP (and similar programming languages).

This approach also stops several escape sequences from being interpreted
accidentally. For example, in PHP it makes a difference whether you write
"\n" or '\n', and “\n” is an escape sequence both in double-quoted string
literals and Extended/Perl-Compatible Regular Expressions.

<http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single>

--
PointedEars
Zend Certified PHP Engineer <http://www.zend.com/en/yellow-pages/ZEND024953>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

bill

unread,
Apr 14, 2017, 11:13:07 AM4/14/17
to
On 4/13/2017 8:06 AM, Tony Mountifield wrote:
> In article <ocnp48$1oei$1...@gioia.aioe.org>,
> bill <wil...@TechServSys.com> wrote:
>> I am trying to strip all non-numeric characters from a string.
>> This is my best guess, but it doesn't do anything to the input
>> string.
>>
>> $number =preg_replace("[^0-9]","",$phone);
>>
>> when $phone is '1a2bc34567890'
>>
>
> The pattern string needs to include delimiters within the string,
> so in your case, something like "/[^0-9]/".
>
> You can also use the specifier \D to mean non-digit:
>
> $phone = '1a2bc34567890';
> $number = preg_replace("/\D/","",$phone);
> echo "$phone => $number";
>
> Produces:
> 1a2bc34567890 => 1234567890
>
> Cheers
> Tony
>

Cheers indeed. Thank you for the explanation.
-bill

bill

unread,
Apr 14, 2017, 11:13:40 AM4/14/17
to
On 4/13/2017 8:51 AM, Thomas 'PointedEars' Lahn wrote:
> Tony Mountifield wrote:
>
>> $number = preg_replace("/\D/","",$phone);
>
> This should be
>
> $number = preg_replace('/\D/', '', $phone);
>
> Although it does not make a difference here, in general I recommend to
> consistently write string literals where no variable expansion is intended
> and where there are no straight apostrophes in the value, in single-quotes
> (straight apostrophes) in PHP (and similar programming languages).
>
> This approach also stops several escape sequences from being interpreted
> accidentally. For example, in PHP it makes a difference whether you write
> "\n" or '\n', and “\n” is an escape sequence both in double-quoted string
> literals and Extended/Perl-Compatible Regular Expressions.
>
> <http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single>
>

Thank you for the good suggestion.

--bill
0 new messages