clarkk clarkk - 6 months ago 9
PHP Question

Create variable from text string with new lines and quotes

I have some contents from a text file. I want to load it into a variable in PHP

I just need to copy/paste the text directly into a variable without escaping all quotes etc

How to do something like this? I'm not looking for a solution with

file_put_content
.

$str = "saldflasdfl

asdklksadlasd "quoted string"


aslkdlsadfkl";

Answer

How about this, which allows you to use unescaped quote marks in your text block:

$str = <<<DATA
saldflasdfl

asdklksadlasd "quoted string"


aslkdlsadfkl
DATA;

Note that it does need to be added with no whitespace indentation, since that would be taken to be part of the string.

However, do note that if your text block gets over a certain size, it is probably cleaner to put it in a text file and read it into a string.