neezer
neezer

Reputation: 20560

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:

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

... but PHP (5.3.2) returns this:

"<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? 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.


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:

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

For @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' }
});

Upvotes: 1

Views: 2540

Answers (3)

fredrik
fredrik

Reputation: 13550

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).

Upvotes: 0

Mike Moore
Mike Moore

Reputation: 7468

Try using stripslashes() after you use Markdown:

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

Read about it here: http://php.net/manual/en/function.stripslashes.php

Upvotes: 2

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

This might be related to magic quotes - you can read about here http://php.net/manual/en/security.magicquotes.php

Upvotes: 0

Related Questions