我有一个错误地包含n\
的字符串(从csv导入)。所以,我只想将n\
替换为\n
。
可能的条件: n\,\n\n, n\\n,n\n\
$string = "hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n";
答案 0 :(得分:3)
使用此:
str_replace("n\","\n",$string);
在这里我们搜索字符串,找到值" n \"然后用" \ n"替换该值。根据您的条件更新此示例。
答案 1 :(得分:3)
试试这个。它将根据您的问题删除所有可能的匹配项: -
$str = "main_string_goes_here";
$replace = "n\,\n\n,n\n\,\nn\,n\\n";
$arr = explode(",",$replace);
foreach($arr as $value)
{
str_replace($value,"\n",$str);
}
快乐编码: - )
答案 2 :(得分:1)
试试这个
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$string = str_replace('n\\n\\','&new*',$string);
$string = str_replace('\\n\\n','&old*',$string);
$string = str_replace('n\\','\\n',$string);
$string = str_replace('&new*','\\n\\n',$string);
echo $string = str_replace('&old*','\\n\\n',$string);
正如其他人所说,将"n\"
替换为"\n"
将无效。你还需要逃避\
反斜杠
答案 3 :(得分:0)
使用此:
<?php
echo $string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);
echo $text = str_replace('n', '', $new_str);
?>
答案 4 :(得分:-1)
你可以试试这个。
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
echo str_replace('n\\', '\n',$string );