0

Hi I have documents what i need to store within a php variable in the same file. The problem is that the code has single and double quotes, so I can not store it within a variable.

//one of many problem examples
$html ="<span onclick="document.getElementById('video-modal').style.display = 'none';">X</span>";

So the question is how can I put this to a variable and keep it in the same file? I tried this qote `, otherwise I am not sure maybe with ob_start or is there an easier way? Backslashes to escape is also not a solution, because in this way I would have to do a lot of manual work to do this. I also want to avoid doing it into a separate file.

AhaEffect
  • 1
  • 1
  • 3
  • 1
    Backslashes are used to escape characters. – user3783243 Mar 27 '21 at 11:59
  • @user3783243 this is no answer, since I have to many html elements to change for this solution.... – AhaEffect Mar 27 '21 at 12:04
  • 3
    You might want to look into [heredoc](https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc). Since heredoc doesn't use quotes, you won't need to modify/escape the contents. – M. Eriksson Mar 27 '21 at 12:05
  • @MagnusEriksson a really good idea. I tried it but somehow it did not work. Here is my example $html = <<X EOT; Any idea? – AhaEffect Mar 27 '21 at 12:49
  • I can't really see if it's correct without seeing the line breaks as well. The important part is to write the contents on a new line after `<< – M. Eriksson Mar 27 '21 at 12:56
  • or why not use addEventListener that way you only need a html element with a class or id.. – Stefan Avramovic Mar 27 '21 at 14:49

0 Answers0