Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php或javascript将所有换行符和制表符替换为各自的\x代码?_Javascript_Php_Replace - Fatal编程技术网

如何使用php或javascript将所有换行符和制表符替换为各自的\x代码?

如何使用php或javascript将所有换行符和制表符替换为各自的\x代码?,javascript,php,replace,Javascript,Php,Replace,您好,我想分别用\n和\t替换所有换行符和制表符 考虑以下代码标记 <!doctype html> <html> <head> <title>Test</title> <meta charset="UTF-8"> </head> <body> </body> </html> 试验 替换后,代码应该如下所示 <!doct

您好,我想分别用\n\t替换所有换行符和制表符

考虑以下代码标记

<!doctype html>
<html>
   <head>
      <title>Test</title>
      <meta charset="UTF-8">
   </head>
   <body>
   </body>
</html>

试验
替换后,代码应该如下所示

<!doctype html>\n<html>\n\t<head>\n\t\t<meta charset=\"utf-8\">\n\t\t<title>Test</title>\n\n\t\t\n\t</head>\n\t<body>\n\t\n\t</body>\n</html>
\n\n\t\n\t\t\n\t\t测试\n\n\t\t\n\t\n\t\n\t\n
如何做到这一点


我们还能扭转这个过程吗?i、 我们能把原始表格拿回来吗。如果是,那怎么办?

你想要的是这样的东西

 <?php
       $str = '
       <!doctype html>
        <html>
           <head>
              <title>Test</title>
              <meta charset="UTF-8">
           </head>
           <body>
           </body>
        </html>
       ';

       echo $str;
       $arr = array("\n","\t","\r");
       echo $str = str_replace($arr, "", $str);
    ?>

请务必查看str\u replace或preg\u replace函数eah。。。但是如何应用它们呢?$v=“abc
d”$v=str_replace(“
,”\r\n“,$v);但是标记中没有
br
标记否,替换后我希望
\n
\t
\r
字符出现。我们能否重新获取原始字符串如果您想将其转换回来,请尝试以下操作:
str\u替换(数组('\n','\t','\r'),数组(“\n',“\t',“\r”),$html)
str_replace(array("\n", "\t", "\r"), array('\n', '\t', '\r'), $html);