Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #17347 > unrolled thread

help with preg_replace

Started bybill <william@TechServSys.com>
First post2017-04-13 07:57 -0400
Last post2017-04-14 11:12 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  help with preg_replace bill <william@TechServSys.com> - 2017-04-13 07:57 -0400
    Re: help with preg_replace tony@mountifield.org (Tony Mountifield) - 2017-04-13 12:06 +0000
      Re: help with preg_replace Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-13 14:51 +0200
        Re: help with preg_replace bill <william@TechServSys.com> - 2017-04-14 11:13 -0400
      Re: help with preg_replace bill <william@TechServSys.com> - 2017-04-14 11:12 -0400

#17347 — help with preg_replace

Frombill <william@TechServSys.com>
Date2017-04-13 07:57 -0400
Subjecthelp with preg_replace
Message-ID<ocnp48$1oei$1@gioia.aioe.org>
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

[toc] | [next] | [standalone]


#17348

Fromtony@mountifield.org (Tony Mountifield)
Date2017-04-13 12:06 +0000
Message-ID<ocnpk2$vb5$1@softins.softins.co.uk>
In reply to#17347
In article <ocnp48$1oei$1@gioia.aioe.org>,
bill  <william@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

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

[toc] | [prev] | [next] | [standalone]


#17349

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-04-13 14:51 +0200
Message-ID<3058894.y9YRR5uL8U@PointedEars.de>
In reply to#17348
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.

[toc] | [prev] | [next] | [standalone]


#17355

Frombill <william@TechServSys.com>
Date2017-04-14 11:13 -0400
Message-ID<ocqouu$ipt$2@gioia.aioe.org>
In reply to#17349
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

[toc] | [prev] | [next] | [standalone]


#17354

Frombill <william@TechServSys.com>
Date2017-04-14 11:12 -0400
Message-ID<ocqott$ipt$1@gioia.aioe.org>
In reply to#17348
On 4/13/2017 8:06 AM, Tony Mountifield wrote:
> In article <ocnp48$1oei$1@gioia.aioe.org>,
> bill  <william@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web