tags:

views:

63

answers:

1

I have a very long javascript (so long that it's too long to escape manually every single problematic character) that I need to return from a method inside a class. Are there way to return (return not echo or print) safely this kind of code?

Thank you very much

+7  A: 

I think you're after HEREDOC syntax or output buffering:

$str = <<<EOD

function javascript() { alert("foo"); }

EOD;

// or

ob_start();
?>
function javascript() { alert("foo"); }
<?php
$str = ob_get_clean();
Greg
+1 For HEREDOC. Output buffering seems like an overkill to me in this case (although I can imagine a some scenarions where it would be The Right Thing).
Vilx-
Never considered ob_get_clean(), thank you I learned something new. Will use EOD though..
0plus1
`EOD` is not part of the syntax. You can use any identifier (if `EOD` appears in your JavaScript, for example.) The identifier consists of alphanumeric characters and/or underscores, but may not begin with a number.
Blixt
Wait I have a problem if i do this: $return .= <<<EOD EOD; I get a syntax error, unexpected $end
0plus1
the "EOD;" has to be on a new line and can't be indented at all
Greg