简体   繁体   English

如何在 php 中传递带有特殊字符的字符串,以便我可以在页面上显示它

[英]How to pass a string with special characters in php so I can display it on page

I have an index (fscrawler) of pdfs, docs and spreadsheets.我有一个 pdf、文档和电子表格的索引 (fscrawler)。 I wrote a php script to search the index and display the contents of the documents (opens up in a modal).我写了一个 php 脚本来搜索索引并显示文档的内容(以模式打开)。 Below is the code to open the modal with the documents details.下面是打开带有文档详细信息的模式的代码。 However I am unable to view the documents,但是我无法查看文件,

在此处输入图像描述

I would like to preserver the documents original format(indentations and bolds).我想保留文档的原始格式(缩进和粗体)。 kindly advice.好心劝告。

    <button type="button" onclick="searchDetails('<?php echo $r['_source']['file']['filename']; ?>',
      '<?php header('Content-Type: text/html; charset=UTF-8');  
        echo htmlentities( $r['_source']['content']); ?>',
      '<?php echo addslashes($r['_source']['path']['real']); ?>',
      '<?php echo $q ?>')"
      class="btn btn-info btn-sm openBtn" data-toggle="modal" > Details </button>

Catching the syntax errors in the browser console is a good first step.在浏览器控制台中捕获语法错误是很好的第一步。 Next, look at the source of the rendered page.接下来,查看渲染页面的来源。 As a PHP developer, your job is to output valid html, css, and in your case JavaScript to get your page to render. As a PHP developer, your job is to output valid html, css, and in your case JavaScript to get your page to render. When you get a Syntax Error in the console, that means JavaScript.当您在控制台中收到语法错误时,这意味着 JavaScript。 It runs browser side so it has to be debugged in the browser.它在浏览器端运行,因此必须在浏览器中进行调试。

What you're trying to do here is write out a JavaScript invocation of the JavaScript searchDetails() function.您在这里要做的是写出 JavaScript searchDetails() function 的 JavaScript 调用。 But what are you hoping to accomplish with the php header call?但是您希望通过 php header调用来完成什么? I dont know what searchDetails does, but header applies to the page php is rendering.我不知道searchDetails做了什么,但header适用于 php 正在呈现的页面。 It wouldn't apply to the file content.它不适用于文件内容。 You can have a Content-Type header for the webpage, but you can't put a header in a JavaScript string.您可以为网页设置 Content-Type header,但不能将 header 放入 JavaScript 字符串中。 You can put the string value of a header in JavaScript but that won't actually describe the content-type of anything to the browser.您可以将 header 的字符串值放在 JavaScript 中,但这实际上不会向浏览器描述任何内容的内容类型。

I suppose htmlentities might be sufficient to expose the content of your files, but pdfs, docs, and spreadsheets are going to look very funky that way.我想 htmlentities 可能足以公开文件的内容,但是 pdf、文档和电子表格这样看起来会很时髦。 not sure exactly what you want it to look like, but you're probably going to want to serve the content of these files with distinct http requests so they can have distinct headers (esp Content-Type).不确定您希望它看起来像什么,但您可能希望通过不同的 http 请求来提供这些文件的内容,以便它们可以具有不同的标头(尤其是 Content-Type)。 I can't make sense of how htmlentities(spreadsheet_content) would be a valuable string to display.我无法理解htmlentities(spreadsheet_content)将如何成为一个有价值的字符串来显示。

Generally speaking, rendering JavaScript from php (or any templating thing) is a pattern I suggest you avoid.一般来说,我建议你避免从 php (或任何模板)渲染 JavaScript 。 Write static JavaScript and have a clear mechanism to expose data to JavaScript.编写 static JavaScript 并有明确的机制将数据暴露给 JavaScript。 It's fraught with difficulties, including being very difficult to write (quoting and escaping for js, php, html all in the same code ) and debug (eg your syntax error), and often fails to be robust in the face of further development.它充满了困难,包括非常难以编写(引用和 escaping 用于 js,php,html 都在同一代码中,并且无法在您的面部进一步开发中进行稳健的调试)和调试。

I would probably generate a json file that described the file data and then would read that through JavaScript and render the page from it with a js library, probably Vue.js.我可能会生成一个描述文件数据的 json 文件,然后通过 JavaScript 读取该文件并使用 js 库(可能是 Vue.js)从中呈现页面。 As I mentioned I would build a separate php resource for serving the file content so I could add a ContentType header on that.正如我所提到的,我将构建一个单独的 php 资源来提供文件内容,因此我可以在其上添加一个 ContentType header。 If you do this, make absolutely sure you're not serving files outside of your index.如果您这样做,请绝对确保您没有在索引之外提供文件。 It might be wise to request files from their position in the index if possible rather than by path to avoid requests exposing eg /etc/shadow.如果可能,最好从索引中的 position 请求文件,而不是通过路径来避免请求暴露,例如 /etc/shadow。

If you want to make webpages think of that as primarily JavaScript centered development.如果您想让网页主要将其视为以 JavaScript 为中心的开发。 Use PHP to generate data but try to keep your HTML and JS as static as possible.使用 PHP 生成数据,但尽量将 HTML 和 JS 保持为static Choose a JavaScript library - do not fall into the trap of thinking raw js is "simpler" or "more straightforward";选择 JavaScript 库——不要陷入认为 raw js 是“更简单”或“更直接”的陷阱; it's not.它不是。 If you don't have a preference I recommend Vue but Angular, React, even d3.js would all work here.如果你没有偏好,我推荐 Vue,但 Angular、React,甚至 d3.js 都可以在这里工作。 Your data contracts will be more explicit and you'll be able to develop the backend and the frontend in isolation, providing some confidence that the backend works as you hack out JavaScript end.您的数据合同将更加明确,您将能够单独开发后端和前端,从而在您破解 JavaScript 端时提供后端工作的信心。 This might seem like a big complication to break things out this way, but if you give it a try, I think you'll experience pretty quickly how much easier it is.以这种方式解决问题似乎很复杂,但如果你试一试,我想你会很快体验到它是多么容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM