Utkarsh Vishnoi
Utkarsh Vishnoi

Reputation: 149

How to replace all newline and tab charcaters with their respective \x code using php or javascript?

Hello I want to replace all newline characters and tab characters with \n and \t respectively.

Consider the following code markup

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

After replacement the code should look like this

<!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>

How to achieve this?

and the also can we reverse this process? i.e can we get back the original form. If yes then How???

Upvotes: 0

Views: 141

Answers (2)

Mark Eriksson
Mark Eriksson

Reputation: 1475

str_replace(array("\n", "\t", "\r"), array('\n', '\t', '\r'), $html);

Upvotes: 2

Meenesh Jain
Meenesh Jain

Reputation: 2528

What you want is something like this

 <?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);
    ?>

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

Upvotes: 0

Related Questions