clarkk clarkk - 1 year ago 62
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


$str = "saldflasdfl

asdklksadlasd "quoted string"


Answer Source

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

$str = <<<DATA

asdklksadlasd "quoted string"


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download