3

I'm sure this is a duplicate but I couldn't find the answer through search.

Here's my code

str_replace([' ',-,\(,\),\*,-,\|,\/], '', $params['telephone']), '0'

and that works. What I want it to be is:

str_replace([' ',-,\(,\),/,|,\*,-,\#,\], '', $params['telephone']), '0'

As soon as I add in # or \ it fails, and escaping them doesn't seem to work. Is there a different way to escape them?

And although it seemingly works, it does not seem to replace the initial space. Could that be the root of my problem?

D. Peter
  • 487
  • 3
  • 12
Eoin
  • 1,413
  • 2
  • 17
  • 32
  • 3
    How can the first line work? Only 1 of the search strings (the space) is quoted. Is this your real code? – jeroen Jun 29 '17 at 09:58

3 Answers3

3

You want to remove a space, -, (, ), *, -, |, / and also \ and # symbols from the string.

You need to pass an array of these chars to str_replace:

$arr = [ '-', '(', ')', '*', '-', '|', '/', '\\', '#'];
$result = str_replace($arr, '', $s);

Or, you may use a regex with preg_replace like this:

$res = preg_replace('~[- ()*|/\\\\#]~', '', $s);

See the PHP demo.

Regex details:

  • ~ is a regex delimiter, required by all preg_ functions
  • - must be put at the start/end of the character class, or escaped if placed anywhere else, to match literal -
  • \ symbol must be matched with 2 literal \s, and that means one needs 4 backslashes to match a single literal backslash
  • Inside a character class, *, (, ), | (and +, ?, ., too) lose there special status and do not need to be escaped
  • If you work with Unicode strings, add u modifier after the last ~ regex delimiter: '~[- ()*|/\\\\#]~u'.

The difference between string replace and preg replace

str_replace replaces a specific occurrence of a literal string, for instance foo will only match and replace that: foo.

preg_replace will perform replacements using a regular expression, for instance /f.{2}/ will match and replace foo, but also fey, fir, fox, f12, f ) etc.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

Use preg_replace()!

You can delete specific Characters this way

$foo = preg_replace('/[yourcharacters]/', '', $foo);

And you can also do it the other Way around (deleting all but the determined Characters) by adding a ^ (see Regex-Tables).

$foo = preg_replace('/[^theonlyallowedcharacters]/', '', $foo);
Bernhard
  • 1,852
  • 11
  • 19
  • Thank you, useful advice on the only allow part. Perhaps I would be better off doing this as I only want numbers. Thanks for taking the time to answer. – Eoin Jun 29 '17 at 15:04
  • Can you help me to understand why have you added `/` before and after when the other answer has `~`? – Eoin Jun 29 '17 at 15:05
  • 1
    You're welcome! It just indicates the Begin and The End of the Regex. If you only want Numbers and you use Regex to filter the Input, you can use /[^0-9]/. Happy Programming :) – Bernhard Jun 29 '17 at 15:14
  • But wait. If `/` means the start/end of a regex then what does `~` do? It appears the same in the other answer. It is referred to as a delimiter. Does this mean I could use multiple regexes by using `~`? – Eoin Jun 30 '17 at 15:22
  • 1
    It is also just a Delimiter in this Context! – Bernhard Jul 01 '17 at 01:14
  • 1
    And no, you can no use multiple Regexes this way. – Bernhard Jul 01 '17 at 02:53
  • Thank you for your knowledge – Eoin Jul 01 '17 at 11:13
0

I think you need to add \ for special characters for your regular expression.

Please, replace with this code,

 ['',\(,\),\/,\|,\*,-,\#,\\]

Instead of yours,

[' ',-,\(,\),/,|,\*,-,\#,\]

I hope it may helpful.

  • I think this is incorrect, I had used special characters and removed the ones that were not necessary by trial and error and by using PHP Storm to validate. Also I needed that space in there :) – Eoin Jun 29 '17 at 15:01