Asked  7 Months ago    Answers:  5   Viewed   60 times

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.

 Answers

34

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;
Wednesday, March 31, 2021
 
jedwards
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
26

Escaping the quotation marks inside a string prevents them from ending the string. For example:

$str = "Hi this is a "broken" string";

Essentially the PHP parser sees multiple statments: "Hi this is a", broken, and "string ". It becomes an invalid line of code.

When the parser encounters the first quotation mark, it knows it's found a string, and it knows that the next quotation mark tells it where the string ends. If you want to have quotation marks inside your string, you need to tell the parser that they aren't the end of the string by escaping them with backslashes in front.

If you start your string with single quotes, ', then you only need to escape single quotes inside your string. Same with double quotes. These two lines are both valid code:

$str = "This string is 'not' broken";
$str = 'This string is also "not" broken';

You just have to watch for whichever one you used to open and close the string.

Wednesday, March 31, 2021
 
Daveel
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
21

We could match strings or quotations with

[^s"]+|"[^"]*"

So we just need to preg_match_all and concatenate the result.


Example:

$str = 'hola hola "pepsi cola" yay';

preg_match_all('/[^s"]+|"[^"]*"/', $str, $matches);

echo implode('', $matches[0]);
// holahola"pepsi cola"yay
Saturday, May 29, 2021
 
superfell
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :