gpt4 book ai didi

javascript - 如何保留从 php 到 javascript 传递字符串的段落格式

转载 作者:行者123 更新时间:2023-11-29 19:24:46 26 4
gpt4 key购买 nike

好的,我们开始吧。我从 MySQL 表数据中提取一个字符串列表,如下所示:

1 Lumbar facet syndrome, bilateral
2 Bilateral knee arthropathy
3 Bilateral hip arthropathy
4 Right shoulder arthropathy, posttraumatic,
5 Chondromalacia of patella
6 Diabetes mellitus with diabetic nephropathy
7 Lower back pain
8 OA

我想保留此格式,因为我需要将此信息放入文本框中。

我正在使用的程序有一个插件系统,允许系统将 javascript 注入(inject)表单页面。所以插件看起来像这样

 function newForm_javascript_onload() {

//This part grabs the data from the MySQL table.
$sql = "SELECT dictation FROM form_dictation WHERE id = ? ORDER BY date DESC LIMIT 1";
$stmDia = array($id);
$list = sqlQuery($sql,$stmDia);
$list = $list['dictation'];

//Here I chop out the part of the text that I need for the text box
$diaBegin = strpos($list, "Active Problems") + 26;
$diaEnd = strpos($list, "Plan") - 4;
$diaLength = strlen($list);
$diaChop = ($diaEnd - $diaLength);
$diaNar = substr($list, $diaBegin, $diaChop);


//This is the part that gets injected into the page
echo "
var diagnoses = '$diaNar'; //This is where formatting breaks the code

//This part should populate the textarea

if(form_diagFac1){
document.getElementById('form_diagFac1').value = diagnoses;
}


";


}

我尝试搜索论坛,但没有人发布这样的问题。

var 在哪里我已经尝试过:

var diagnosis  = escape('$diaNar'); //This is where php passes data to java

我知道我可以去掉所有格式,使其显示为一个段落。

但是,我需要创建一个读取每一行的循环吗?

最佳答案

尝试json_encode。它将“\r\n”添加到字符串中而不是换行符中,而不破坏它,并将变量输出为单个字符串。

echo "var diagnoses = " . json_encode($diaNar) . ";

关于javascript - 如何保留从 php 到 javascript 传递字符串的段落格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42228503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com