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?
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?
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
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);