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?

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

    回答1:

    72

    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;
    
    

    share|improve this answer

    edited Nov 3 '11 at 18:16

    answered Nov 3 '11 at 17:21

    MoarCodePlzMoarCodePlz 3,94022 gold badges1919 silver badges3030 bronze badges

    add a comment |

    回答2:

    47

    Use the addslashes function:

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

    share|improve this answer

    edited Jul 8 at 14:56

    Peter Mortensen 14.6k1919 gold badges8989 silver badges118118 bronze badges

    answered Oct 18 '13 at 9:58

    MatoeilMatoeil 3,75777 gold badges3939 silver badges6363 bronze badges

    • 1

      it's strange ..i was having trouble with exact same word :D. Thanks – Latheesh V M Villa Sep 13 '18 at 4:57

    add a comment |

    回答3:

    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.

    share|improve this answer

    edited Jul 8 at 14:57

    Peter Mortensen 14.6k1919 gold badges8989 silver badges118118 bronze badges

    answered Nov 3 '11 at 18:03

    Your Common SenseYour Common Sense 137k2121 gold badges154154 silver badges269269 bronze badges

    • 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 3 '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 3 '11 at 18:39

    add a comment |

    回答4:

    2

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

    or

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

    share|improve this answer

    answered Nov 3 '11 at 17:22

    ManseUKManseUK 34.9k88 gold badges6666 silver badges9696 bronze badges

    add a comment |

    回答5:

    1

    Either escape the quote:

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

    or use single quotes to denote your string:

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

    share|improve this answer

    edited Jun 10 '17 at 16:13

    nyedidikeke 3,55766 gold badges2424 silver badges3939 bronze badges

    answered Nov 3 '11 at 17:22

    Manos DilaverakisManos Dilaverakis 4,92144 gold badges2424 silver badges5656 bronze badges

    • wrong answer. please correct it – Ariful Islam Nov 3 '11 at 17:23
    • The problem is that I have this string. 'From time to "time", the boy would get 'confused'' it gives an error – user478636 Nov 3 '11 at 17:27

    add a comment |

    回答6:

    1

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

    share|improve this answer

    edited Jul 8 at 14:58

    Peter Mortensen 14.6k1919 gold badges8989 silver badges118118 bronze badges

    answered Nov 3 '11 at 17:22

    rogerlsmithrogerlsmith 5,29222 gold badges1717 silver badges2525 bronze badges

    • 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 3 '11 at 17:25

    add a comment |

    回答7:

    1

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

    share|improve this answer

    answered Sep 24 at 1:52

    jarodjarod 5111 silver badge44 bronze badges

    add a comment |


Log in to reply
 

最新内容

  • My .gitignore file contains these lines :

    xcuserdata/**/* !xcuserdata/**/xcschemes/*

    But the following file is still tracked /MyApp/MyApp.xcodeproj/xcuserdata/colas.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist

    Why is it so? How can I fix that?

    PS: If I had MyApp.xcodeproj/xcuserdata/colas.xcuserdatad/xcdebugger, the files are ignored. But I don't understand why it does not ignore them without this "hack".

    EDIT 1

    Contrary to what is said in one of the answer, the pattern

    xcuserdata/**/* !xcuserdata/**/xcschemes/*

    works !!! I mean, the files under /xcschemes are tracked.

    See also the post Git ignore file for Xcode projects, where I get this .gitignore file.

    EDIT 2

    My Git version is 1.8.3.4 (Apple Git-47).

    EDIT 3

    When I git check-ignore this file, here is what I get

    fatal: Not a git repository (or any of the parent directories): .git

    But the fact is that a parent directory is a git directory...

    EDIT 4

    When I git check-ignore --no-index -- this file, here is what I get

    [MT] PluginLoading: Required plug-in compatibility UUID 37B30044-3B14-46BA-ABAA-F01000C27B63 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeSnippetsHelper.xcplugin' not present in DVTPlugInCompatibilityUUIDs 2014-02-10 10:03:50.856 xcodebuild[1496:d07] XcodeColors: load (v10.1) 2014-02-10 10:03:50.859 xcodebuild[1496:d07] XcodeColors: pluginDidLoad: fatal: Not a git repository (or any of the parent directories): .git EDIT 4bis

    From the root folder :

    if I don't use the no-index option, there is no reply to git check-ignore. if I use the no-index option: I get the error error: unknown option 'no-index'...

    Strange ;-!

    First, you can do a git check-ignore (git 1.8.3.3+) to see what rule is ignoring your file (assuming it wasn't in the index in the first place)

    Second, read ".gitignore exclude folder but include specific subfolder":

    It is not possible to re-include a file if a parent directory of that file is excluded. (*)
    (*: unless certain conditions are met in git 2.8+, see below)
    Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

    So the file of xcschemes wouldn't be un-ignored anyway.
    You needed to ignore parent folder per parent folder.
    But a better approach is to ignores files only, and then exclude the folder:

    xcuserdata/** !xcuserdata/**/ !xcuserdata/**/xcschemes/**

    Remember:

    You need to exclude folders from the gitignore rules before being able to exclude files.

    Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

    Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

    commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0 commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.

    However:

    The directory part in the re-include rules must be literal (i.e. no wildcards)

    So that wouldn't have worked here anyway.

    IanH

    Adding file names to .gitignore helps prevent you from adding the files unintentionally. If you do something like git add . to add a folder full of files to the repository, the files (or filetypes) ignored in .gitignore will not be added. They would have to be added by using the -f flag with the git add command. It will not change anything if the files are already tracked.

    You can stop tracking the files by using the answer to the question: Remove a file from a Git repository without deleting it from the local filesystem

    edit: After rereading the question a few more times, I think you may be having roughly the same issue that was addressed in this answer: .gitignore - ignore any 'bin' directory

    来源:https://stackoverflow.com/questions/21643583/why-are-these-files-not-ignored-by-git

    read more
  • 问题

    In Submitting Applications in the Spark docs, as of 1.6.0 and earlier, it\'s not clear how to specify the --jars argument, as it\'s apparently not a colon-separated classpath not a directory expansion.

    The docs say \"Path to a bundled jar including your application and all dependencies. The URL must be globally visible inside of your cluster, for instance, an hdfs:// path or a file:// path that is present on all nodes.\"

    Question: What are all the options for submitting a classpath with --jars in the spark-submit script in $SPARK_HOME/bin? Anything undocumented that could be submitted as an improvement for docs?

    I ask because when I was testing --jars today, we had to explicitly provide a path to each jar:

    /usr/local/spark/bin/spark-submit --class jpsgcs.thold.PipeLinkageData ---jars=local:/usr/local/spark/jars/groovy-all-2.3.3.jar,local:/usr/local/spark/jars/guava-14.0.1.jar,local:/usr/local/spark/jars/jopt-simple-4.6.jar,local:/usr/local/spark/jars/jpsgcs-core-1.0.8-2.jar,local:/usr/local/spark/jars/jpsgcs-pipe-1.0.6-7.jar /usr/local/spark/jars/thold-0.0.1-1.jar

    We are choosing to pre-populate the cluster with all the jars in /usr/local/spark/jars on each worker, it seemed that if no local:/ file:/ or hdfs: was supplied, then the default is file:/ and the driver makes the jars available on a webserver run by the driver. I chose local, as above.

    And it seems that we do not need to put the main jar in the --jars argument, I have not tested yet if other classes in the final argument (application-jar arg per docs, i.e. /usr/local/spark/jars/thold-0.0.1-1.jar) are shipped to workers, or if I need to put the application-jar in the --jars path to get classes not named after --class to be seen.

    (And granted with Spark standalone mode using --deploy-mode client, you also have to put a copy of the driver on each worker but you don\'t know up front which worker will run the driver)

    回答1:

    7

    In this way it worked easily.. instead of specifying each jar with version separately..

    #!/bin/sh # build all other dependent jars in OTHER_JARS JARS=`find ../lib -name '*.jar'` OTHER_JARS="" for eachjarinlib in $JARS ; do if [ "$eachjarinlib" != "APPLICATIONJARTOBEADDEDSEPERATELY.JAR" ]; then OTHER_JARS=$eachjarinlib,$OTHER_JARS fi done echo ---final list of jars are : $OTHER_JARS echo $CLASSPATH spark-submit --verbose --class <yourclass> ... OTHER OPTIONS --jars $OTHER_JARS,APPLICATIONJARTOBEADDEDSEPERATELY.JAR Using tr unix command also can help like the below example.

    --jars $(echo /dir_of_jars/*.jar | tr ' ' ',')

    share|improve this answer

    edited Jun 2 at 1:50

    answered Feb 22 '16 at 9:36

    Ram GhadiyaramRam Ghadiyaram 22.2k99 gold badges6464 silver badges9191 bronze badges

    This is a nice workaround, for Spark 1.6.1 and later, when available, the comma list requirement should be documented more directly. I went ahead and accepted this. Just to be sure, this works as is? The line OTHER_JARS=$eachjarinlib,$OTHER_JARS doesn't need any quotes? I guess the shell is not going to expand that comma out and therefore takes it just to be a string, but it might be safer to double quote it? OTHER_JARS="$eachjarinlib,$OTHER_JARS"? – JimLohse Feb 22 '16 at 14:22

    1

    Jim, Yes doesn't need double quotes. This works(spark 1.3,1.5 versions) as it is. – Ram Ghadiyaram Feb 23 '16 at 6:39

    4

    this is another option which is simpler --jars $(echo /dir/of/jars/*.jar | tr ' ' ',') – Ram Ghadiyaram Feb 29 '16 at 7:11

    add a comment |

    回答2:

    0

    One way (the only way?) to use the --jars argument is to supply a comma-separated list of explicitly named jars. The only way I figured out to use the commas was a StackOverflow answer that led me to look beyond the docs to the command line:

    spark-submit --help

    The output from that command contains:

    --jars JARS Comma-separated list of local jars to include on the driver and executor classpaths.

    Today when I was testing --jars, we had to explicitly provide a path to each jar:

    /usr/local/spark/bin/spark-submit --class jpsgcs.thold.PipeLinkageData ---jars=local:/usr/local/spark/jars/groovy-all-2.3.3.jar,local:/usr/local/spark/jars/guava-14.0.1.jar,local:/usr/local/spark/jars/jopt-simple-4.6.jar,local:/usr/local/spark/jars/jpsgcs-core-1.0.8-2.jar,local:/usr/local/spark/jars/jpsgcs-pipe-1.0.6-7.jar /usr/local/spark/jars/thold-0.0.1-1.jar

    share|improve this answer

    answered Jan 12 '16 at 8:04

    JimLohseJimLohse 1,02111 gold badge1313 silver badges3434 bronze badges

    add a comment |

    read more
  • One way (the only way?) to use the --jars argument is to supply a comma-separated list of explicitly named jars. The only way I figured out to use the commas was a StackOverflow answer that led me to look beyond the docs to the command line:

    spark-submit --help

    The output from that command contains:

    --jars JARS Comma-separated list of local jars to include on the driver and executor classpaths.

    Today when I was testing --jars, we had to explicitly provide a path to each jar:

    /usr/local/spark/bin/spark-submit --class jpsgcs.thold.PipeLinkageData ---jars=local:/usr/local/spark/jars/groovy-all-2.3.3.jar,local:/usr/local/spark/jars/guava-14.0.1.jar,local:/usr/local/spark/jars/jopt-simple-4.6.jar,local:/usr/local/spark/jars/jpsgcs-core-1.0.8-2.jar,local:/usr/local/spark/jars/jpsgcs-pipe-1.0.6-7.jar /usr/local/spark/jars/thold-0.0.1-1.jar

    read more

最新主题

179
Online

336
Users

219.4k
Topics

369.9k
Posts

在线用户

推荐阅读

本站部分内容来自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请联系我们,我们将及时予以处理。
Powered by NodeBB | 备案号:宁ICP备15000671号