Home >Backend Development >PHP Tutorial >PHP implements formatting multi-line text into JS usable format, _PHP tutorial

PHP implements formatting multi-line text into JS usable format, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:04790browse

php implements formatted multi-line text into a usable format for Js.

There are many scenarios for storing and writing templates in js, such as: html code templates for pop-up boxes, etc. js does not Long text writing that supports line breaks must be added up line by line, such as:

Copy code The code is as follows:
var content = 'dc6dce4a544fdca2df29d5ac0ea9906brow 116b28748ea4df4d9c2150843fecfba68'
'dc6dce4a544fdca2df29d5ac0ea9906brow 216b28748ea4df4d9c2150843fecfba68';

but cannot be written as:

Copy code The code is as follows:
var content = 'dc6dce4a544fdca2df29d5ac0ea9906b row 116b28748ea4df4d9c2150843fecfba68
dc6dce4a544fdca2df29d5ac0ea9906brow2 16b28748ea4df4d9c2150843fecfba68 ';

So I processed a small piece of php code to simplify the manual typing operation.

tojs.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
  <head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
   <title>Str To Js String</title>
    <style type="text/css">
.content-box { border: 1px #f0f0f0 slid; border-left: 4px #e0e0e0 solid; padding: 5px 5px 5px 10px; }
</style>
  </head>
  <body>
<h1>输入格式化的文本:</h1>
<&#63;php 
$jsContent = '';
if(isset($_POST['content']) && $_POST['content']) { 
  $content  = strtr(htmlspecialchars($_POST['content']), array("\r\n" => "\n"));
  $rows    = explode("\n", $content);
  foreach($rows as $row) {
    $jsContent .= '+ \'' . $row . "'<br/>";
  }
  $jsContent{0}  = ' ';
} 
&#63;>
    <form action="#" method="post">
      <textarea name="content" style="width: 99%;height: 300px; "></textarea>
      <p><input type="submit" value="提交" /></p>
    </form>
<h2>格式化后的结果:</h2>
<div class="content-box">
  <&#63;php echo $jsContent;&#63;>
</div>
  </body>
</html>

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/984618.htmlTechArticlephp implements formatting multi-line text into a usable JS format. There are many scenarios for storing and writing templates in JS, such as: HTML code templates for pop-up boxes, etc. JS does not support long text writing with line breaks, so you must...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn