-1

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?

TheRed27
  • 25
  • 12

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
  • 123,192
  • 20
  • 49
  • 81
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
  • 37,596
  • 11
  • 75
  • 105