Ereg_replace到preg_replace(“\ n”,“\\ n”,$ row [$ j])到preg_replace

时间:2013-07-30 05:32:27

标签: php regex preg-replace ereg-replace

在php 5.4中使用ereg_replace函数时,我收到了一个弃用的函数警告。

我想将ereg_replace转换为preg_replace

以下是功能:ereg_replace("\n","\\n",$row[$j]);

同样的preg_replace可能是什么?

3 个答案:

答案 0 :(得分:3)

您的正则表达式只是一个文字字符串,因此根本不需要使用正则表达式函数。将其更改为:

str_replace("\n", '\n', $row[$j]);

转义序列在双引号内处理,但不在单引号内处理。

答案 1 :(得分:1)

$string = 'Hello there is a special character \n in this string';
$pattern = '/\\\n/';
$replacement = '\\\\\n';
echo preg_replace($pattern, $replacement, $string);

由于\ n是特殊字符,您必须为每个字符使用\。 '\ n'使用'\\ n'和'\ n'使用'\\\ n'

答案 2 :(得分:0)

您可以按照以下方式使用它。

$row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
相关问题