Home > php教程 > php手册 > php实现格式化多行文本为Js可用格式,

php实现格式化多行文本为Js可用格式,

WBOY
Release: 2016-06-13 09:06:57
Original
931 people have browsed it

php实现格式化多行文本为Js可用格式,

js里现在存写模板的场景好多,如:弹框类的html代码模板等,js不支持换行的长文本写法,必需要一行行的加起来,如:

复制代码 代码如下:
var content = '

row 1
'
+ '
row 2
';

而不可以写成:

复制代码 代码如下:
var content = '

row 1

row2
';

于是小加工一php小段代码,简化手工打的操作。

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>
Copy after login

以上所述就是本文的全部内容了,希望大家能够喜欢。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template