Escaping quotation marks in PHP

  • 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?

        $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.

  • 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: 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.

  • Use the addslashes function:

     $str = "Is your name O'Reilly?";
     // Outputs: Is your name O\'Reilly?
       echo addslashes($str);

  • 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.

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


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

  • Either escape the quote:

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

    or use single quotes to denote your string:

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

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

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

Log in to reply


  • Is neural network=Tree data structure?.If not how neural networks r described using physical model?

    For ex:An array is considered as collection of similar data under same name(sequential memory allocation=physical model)

    read more
  • I have cloned the fabric samples repository and am running the byfn script to build the first network. I am using the node js SDK for application development. I have written a chain code function which does a time-consuming computation and the returns a true/false value to the middleware.

    I have installed my chain-code in only peer0 from Org1.

    Unfortunately, I get a timeout there is no response from the chaincode for ~30seconds. The error I get is shown below:

    warn: [Query]: evaluate: Query ID \"[object Object]\" of peer \"\" failed: message=failed to execute transaction 799eb959954a7f2f8f75dee735969a4ba374b4bc98b4bbacd2fc85fc57a860b9: error sending: timeout expired while executing transaction, stack=Error: failed to execute transaction 799eb959954a7f2f8f75dee735969a4ba374b4bc98b4bbacd2fc85fc57a860b9: error sending: timeout expired while executing transaction at self._endorserClient.processProposal (/home/jacktheripper/fabric-samples/zeroknowledge/snarkjs/node_modules/fabric-client/lib/Peer.js:140:36) at Object.onReceiveStatus (/home/jacktheripper/fabric-samples/zeroknowledge/snarkjs/node_modules/grpc/src/client_interceptors.js:1207:9) at InterceptingListener._callNext (/home/jacktheripper/fabric-samples/zeroknowledge/snarkjs/node_modules/grpc/src/client_interceptors.js:568:42) at InterceptingListener.onReceiveStatus (/home/jacktheripper/fabric-samples/zeroknowledge/snarkjs/node_modules/grpc/src/client_interceptors.js:618:8) at callback (/home/jacktheripper/fabric-samples/zeroknowledge/snarkjs/node_modules/grpc/src/client_interceptors.js:845:24), status=500, , url=grpcs://localhost:7051,, grpc.max_receive_message_length=-1, grpc.max_send_message_length=-1, grpc.keepalive_time_ms=120000, grpc.http2.min_time_between_pings_ms=12000, grpc.keepalive_timeout_ms=2000000, grpc.http2.max_pings_without_data=5, grpc.keepalive_permit_without_calls=1,,,, isProposalResponse=true

    Inorder to resolve this, I figured that I would have to overide the default timeout configurations in fabric. SO, I made changes to the deafault.jsonconfig file inside the fabric-client node module.I increased the values inside the keys of \"request-timeout\",\"grpc.keepalive_timeout_ms\", \"grpc-wait-for-ready-timeout\" but my applicationstill times out in like ~30-35 seconds. I also changes the timeout parameter value in the connection-org1.yamlfile but the application still times out at the same time.

    How do I increase this timeout time to allow the heavy computation inside my chaincode? I know that my chaincode and fabric installations are correct because because the same applciationa and chaincode runs finein a system with better resources(i.e the computation takes ~15 seconds in faster computer)

    read more

    I wanted to use the jquery gridnav library but it was having some conflict with my jquery version. so i renamed the deprecated code with latest one. So it removed the conflict and error messages. But only the navigation and animation is ot happening.

    Given below is the changed code of jquery gridnav. Code commented is the old code

    <script type=\"text/javascript\" src=\"js/jquery.easing.1.3.js\"></script> <script type=\"text/javascript\" src=\"js/jquery.gridnav.js\"></script> <script type=\"text/javascript\"> $(function() { $(\'#tj_container\').gridnav({ type : { mode : \'disperse\', speed : 500, easing : \'\', factor : \'\', reverse : \'\' } }); }); </script> //$(\'<img/>\').load( function() { $(\'<img/>\').on(\'load\',function() { //$p_nav.bind(\'click.gridnav\', function( e ) { $p_nav.on(\'click\', function( e ) { //$n_nav.bind(\'click.gridnav\', function( e ) { $n_nav.on(\'click\', function( e ) { $.on( \"error\", \"Method \" + method + \" does not exist on jQuery.gridnav\"); //$.error( \'Method \' + method + \' does not exist on jQuery.gridnav\' );

    read more



Powered by NodeBB | 备案号:宁ICP备15000671号