简体   繁体   English

防止 PHP 自动返回 escaping 字符串?

[英]Prevent PHP from automatically escaping returned string?

I have a function that returns a string of HTML code for a jQuery plugin, parsed using PHP Markdown : I have a function that returns a string of HTML code for a jQuery plugin, parsed using PHP Markdown :

function awesome_function() {
  $html = Markdown('# Hello World!');
  return $html;
}

... but PHP (5.3.2) returns this: ...但是 PHP (5.3.2) 返回:

"<h1>Hello World!<\/h1>\n"

... instead of what I want, which is this: ...而不是我想要的,这是:

<h1>Hello World!</h1>

How do I get it to return the non-escaped HTML value?如何让它返回非转义的 HTML 值? Changing return to echo works, but then I have the return status of the function returned as well (true|false|null), which I don't want.return更改为echo有效,但随后我也返回了 function 的return状态(true|false|null),这是我不想要的。


EDIT: Not sure if it's relevant, but I just realized that I probably should have mentioned that my function is a public function for a class, as such:编辑:不确定它是否相关,但我刚刚意识到我可能应该提到我的 function 是 class 的公共 function,例如:

class Awesome {
  public function awesome_function() { /* ... */ }
}

For @fredrick:对于@fredrick:

$('.edit').editable('/controllers/awesome_controller.php', {
  id: 'identifier',
  name: 'content',
  submitdata: { action: 'awesome_function' },
  submit: 'Ok',
  cancel: 'Cancel',
  loadurl: '/controllers/awesome_controller.php',
  loaddata: { action: 'load_original_markdown' }
});

Try using stripslashes() after you use Markdown :在使用Markdown后尝试使用stripslashes()

function awesome_function() {
  $html = Markdown('# Hello World!');
  $html = stripslashes($html);
  return $html;
}

Read about it here: http://php.net/manual/en/function.stripslashes.php在这里阅读: http://php.net/manual/en/function.stripslashes.php

This might be related to magic quotes - you can read about here http://php.net/manual/en/security.magicquotes.php这可能与魔术引号有关-您可以在此处阅读http://php.net/manual/en/security.magicquotes.php

As we figured out, you have something like this in your code:正如我们所知道的,您的代码中有这样的内容:

$awesome = new Awesome();
echo json_encode($awesome->awesome_function());

json_encode will add slashes to the output, and when you echo instead of returning from awesome_function, two values will be echoed, the HTML, and also the json_encoded response from the awesome_function (true|false|null). json_encode 将为 output 添加斜线,当您回显而不是从 awesome_function 返回时,将回显两个值,即 HTML,以及来自 awesome_function 的 json_encoded 响应 (true|false|null)。

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

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