0

I need to convert an example string contains, for example: "ABF965;CDE436;EAF873" in "N;N;N" where all alphanumerics characters must be "N"

Is there any native function to do this?

xXConcasXx
  • 91
  • 11

2 Answers2

1

You can use preg_replace, using a regex to convert all sequences of alphanumeric characters to a single N:

$str = "ABF965;CDE436;EAF873";
echo preg_replace('/[A-Za-z0-9]+/', 'N', $str);

Output

N;N;N

Demo on 3v4l.org

Nick
  • 118,076
  • 20
  • 42
  • 73
1

Simple, replace one or more occurrences of non-semicolon strings. This is done most simply by using a negated character class.

Code: (Demo)

$string = "ABF965;CDE436;EAF873";
echo preg_replace('/[^;]+/', 'N', $string);

Output:

N;N;N

More literally, given your sample, an alphanumeric character class with a one-or-more quantifier.

echo preg_replace('/[A-Z\d]+/', 'N', $string);
mickmackusa
  • 33,121
  • 11
  • 58
  • 86