How can I preg_replace with special characters, numbers and everything?

$pattern = ‘/^([0-9]{3})001([0-9]{0,})$/’;
$replacement = ‘/^([0-9]{3})001([0-9]{0,})$/’;
$subject = ‘CLP520001103480891775486.2**1202014282509832Z0X00221~
NM1
QC1BALLWANDAMIR59915931~
NM1
IL
1BALLEDWARDMIR59915931~
NM1
82
1
VENKATARAMANSUBRAMANIANXX1154347748~
REF
1L
0000FEP000111~’;
preg_replace($pattern, $subject, $replacement)
echo $subject;

How can I do that? I just want the number in the beginning to have a “-” after the 001

Wouldn’t a simple str_replace() do the trick then? Like

str_replace('001', '001-', $subject);

maybe with the optional $count parameter if you only want to replace the first match.

BTW, if you want to use regular expressions, only the pattern is a regex; the replacement is a string where you can access captured matches like “$1” (say).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.