72

I am getting a parse error, and I think it's because of the quotation marks over "time". How can I make it treat it as a whole string?

<?php
    $text1 = 'From time to "time" this submerged or latent theater in 'Hamlet'
    becomes almost overt. It is close to the surface in Hamlet's pretense of madness,
    the "antic disposition" he puts on to protect himself and prevent his antagonists
    from plucking out the heart of his mystery. It is even closer to the surface when
    Hamlet enters his mother's room and holds up, side by side, the pictures of the
    two kings, Old Hamlet and Claudius, and proceeds to describe for her the true
    nature of the choice she has made, presenting truth by means of a show.
    Similarly, when he leaps into the open grave at Ophelia's funeral, ranting in
    high heroic terms, he is acting out for Laertes, and perhaps for himself as well,
    the folly of excessive, melodramatic expressions of grief.";

    $text2 = 'From time to "time"';

    similar_text($textl, $text2, $p);
    echo "Percent: $p%";

The problem is that I can't manually add \ before every quotation mark. This is the actual text I need to compare.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user478636
  • 3,304
  • 15
  • 49
  • 76
  • 2
    Manual here: http://php.net/manual/en/language.types.string.php - Escaping is the same as in most other C-style languages. – mario Nov 03 '11 at 17:25
  • 1
    Actually, the problem in the screenshot above begins with the single-quote before the word Hamlet, as the coloring suggest. Since the string is started with a single quote, all the single quotes inside must be escaped, except the last one. So, the first variable declaration should begin like this : `'From time to "time" this ... theater in \'Hamlet\'...` The $text2 variable is actually fine. – Goozak Oct 06 '16 at 14:17

7 Answers7

92

Use a backslash as such

"From time to \"time\"";

Backslashes are used in PHP to escape special characters within quotes. As PHP does not distinguish between strings and characters, you could also use this

'From time to "time"';

The difference between single and double quotes is that double quotes allows for string interpolation, meaning that you can reference variables inline in the string and their values will be evaluated in the string like such

$name = 'Chris';
$greeting = "Hello my name is $name"; //equals "Hello my name is Chris"

As per your last edit of your question I think the easiest thing you may be able to do that this point is to use a 'heredoc.' They aren't commonly used and honestly I wouldn't normally recommend it but if you want a fast way to get this wall of text in to a single string. The syntax can be found here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc and here is an example:

$someVar = "hello";
$someOtherVar = "goodbye";
$heredoc = <<<term
This is a long line of text that include variables such as $someVar
and additionally some other variable $someOtherVar. It also supports having
'single quotes' and "double quotes" without terminating the string itself.
heredocs have additional functionality that most likely falls outside
the scope of what you aim to accomplish.
term;
MoarCodePlz
  • 5,086
  • 2
  • 25
  • 31
67

Use the addslashes function:

 $str = "Is your name O'Reilly?";

 // Outputs: Is your name O\'Reilly?
   echo addslashes($str);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matoeil
  • 6,851
  • 11
  • 54
  • 77
  • 1
    it's strange ..i was having trouble with exact same word :D. Thanks –  Sep 13 '18 at 04:57
  • 1
    This should be the accepted answer since it is valid both to php hard-coded text, and to validate user-inputs. +1 – Sergio A. Jan 16 '20 at 08:50
9

Use htmlspecialchars(). Then quote and less / greater than symbols don't break your HTML tags~

jarod
  • 131
  • 2
  • 4
5

Save your text not in a PHP file, but in an ordinary text file called, say, "text.txt"

Then with one simple $text1 = file_get_contents('text.txt'); command have your text with not a single problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Actually, I will have a function that returns the text from a doc file as such $text1 = readDoc("abc.docx"); // returns text Will this be fine? – user478636 Nov 03 '11 at 18:21
  • Sorry it was my mistake, I thought you could use @ or some other character before the beginning of the string like in c#. Anyways it works. – user478636 Nov 03 '11 at 18:39
3
$text1= "From time to \"time\"";

or

$text1= 'From time to "time"';
Manse
  • 37,765
  • 10
  • 83
  • 108
1

Either escape the quote:

$text1= "From time to \"time\"";

or use single quotes to denote your string:

$text1= 'From time to "time"';
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Manos Dilaverakis
  • 5,849
  • 4
  • 31
  • 57
  • The problem is that I have this string. 'From time to "time", the boy would get 'confused'' it gives an error – user478636 Nov 03 '11 at 17:27
1

You can use the PHP function addslashes() to any string to make it compatible

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rogerlsmith
  • 6,670
  • 2
  • 20
  • 25
  • tbe problem is that i have a large text with many "" marks, and its giving parse error. I need this string as i will use it in similar_text() function – user478636 Nov 03 '11 at 17:25