Home php Escaping php characters

Escaping php characters

Author

Date

Category

The question is as follows:
There are several lines of code to send via json (i.e. string). Since in html “+” is a concatenation sign, the markup language simply eats it up and the plus sign is lost. Dealt with this. With quotes, I could not completely.
Example 1 (everything is OK):

$ text = 'Some "hard" text';

because quotes in different formats, skips.
But if you take example 2:

$ text = 'Some' hard 'text';

php swears because thinks the quote before hard is the end of the string.
Actually, it is not even possible to create the text in this form.
The approximate code is as follows:

$ text = 'Some "ha' + 'rd" text';
$ patterns = array ("/ \ + /", "/ \ '/", "/ \’ / "," / \ “/", "/ \” / ");
$ replacements = array ("% 2B", "% 91", "% 92", "% 93", "% 94");
$ text = preg_replace ($ patterns, $ replacements, $ text);

The text is taken from the input text field, and no one will insert “\” before each character.
How to make it possible to contain quotation marks in the text – the same as those that serve as the beginning and end of a line?
those. like this:

$ text = 'Some' hard 'text';

Thanks in advance.


Answer 1, authority 100%

In PHP, “+” means append (operation with numbers), concatenation is denoted by “.” (period), and if you want to use quotes in the same quotes, just put a backslash in front of them:

$ text = 'Some \' hard \ 'text';

Answer 2

Thanks, but it was not quite right. Solved with heredoc syntax:

$ bar = & lt; & lt; & lt; EOT
"Double quotes" 'Single' and a bunch of characters & lt; & gt;?! @ # $% ^ & Amp; * () _ + _ =
EOT;
echo $ bar;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions