关闭preg_replace中的g(全局)修饰符

The g (global) modifier is on by default in preg_replace PHP, how can it be switched off ?

If I do a preg_replace(/n/,'',$string); it will replace all occurrences of n but I want to replace only the first occurrence of n. How should I do it?

Use the fourth parameter for preg_replace(). From the documentation:

limit — The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

preg_replace('/n/', '', $string, 1);

But for something simple as this, a regex is overkill. A simple str_replace() should suffice:

$string = str_replace('n', '', $string, 1);