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
 

最新内容

  • I have a Resharper 9x license and it is only compatible up to MS VS Community 2015 edition. I tried to download the 2015 version from Microsoft but their website stubbornly wants me to have 2017, only. Can someone please provide me a url to the 2015 edition? Or, explain how to navigate Microsoft's site to obtain the 2015 edition?

    Update on solution

    1) vs_community.exe is the download filename for any version, 2015, 2017, etc. You will not know which version it is until you click the .exe. You will then see the version in the title of the installer.

    2) There is a url trick to get the version that you want.

    https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel=15

    ...change the rel=15 to rel=17 etc

    Or use the links provided in the answer below

    You can use these links to download Visual Studio 2015

    Community Edition:

    Web Installer ISO Image

    And for anyone in the future who might be looking for the other editions here are the links for them as well:

    Professional Edition:

    Web Installer ISO Image

    Enterprise Edition:

    Web Installer ISO Image

    The "official" way to get the vs2015 is to go to https://my.visualstudio.com/ ; join the " Visual Studio Dev Essentials" and then search the relevant file to download https://my.visualstudio.com/Downloads?q=Visual Studio 2015 with Update 3

    来源:https://stackoverflow.com/questions/44290672/how-to-download-visual-studio-community-edition-2015-not-2017

    read more
  • Briefly: Does anyone know of a GUI for gdb that brings it on par or close to the feature set you get in the more recent version of Visual C++?

    In detail: As someone who has spent a lot of time programming in Windows, one of the larger stumbling blocks I've found whenever I have to code C++ in Linux is that debugging anything using commandline gdb takes me several times longer than it does in Visual Studio, and it does not seem to be getting better with practice. Some things are just easier or faster to express graphically.

    Specifically, I'm looking for a GUI that:

    Handles all the basics like stepping over & into code, watch variables and breakpoints Understands and can display the contents of complex & nested C++ data types Doesn't get confused by and preferably can intelligently step through templated code and data structures while displaying relevant information such as the parameter types Can handle threaded applications and switch between different threads to step through or view the state of Can handle attaching to an already-started process or reading a core dump, in addition to starting the program up in gdb

    If such a program does not exist, then I'd like to hear about experiences people have had with programs that meet at least some of the bullet points. Does anyone have any recommendations?

    Edit:
    Listing out the possibilities is great, and I'll take what I can get, but it would be even more helpful if you could include in your responses:
    (a) Whether or not you've actually used this GUI and if so, what positive/negative feedback you have about it.
    (b) If you know, which of the above-mentioned features are/aren't supported

    Lists are easy to come by, sites like this are great because you can get an idea of people's personal experiences with applications.

    Vicent Marti

    You won't find anything overlaying GDB which can compete with the raw power of the Visual Studio debugger. It's just too powerful, and it's just too well integrated inside the IDE.

    For a Linux alternative, try DDD if free software is your thing.

    Eclipse CDT will provide an experience comparable to using Visual Studio. I use Eclipse CDT on a daily basis for writing code and debugging local and remote processes.

    If your not familiar with using an Eclipse based IDE, the GUI will take a little getting used to. However, once you get to understand the GUI ideas that are unique to Eclipse (e.g. a perspective), using the tool becomes a nice experience.

    The CDT tooling provides a decent C/C++ indexer that allows you to quickly find references to methods in your code base. It also provides a nice macro expansion tool and limited refactoring support.

    With regards to support for debugging, CDT is able to do everything in your list with the exception of reading a core dump (it may support this, but I have never tried to use this feature). Also, my experience with debugging code using templates is limited, so I'm not sure what kind of experience CDT will provide in this regard.

    For more information about debugging using Eclipse CDT, you may want to check out these guides:

    Interfacing with the CDT debugger, Part 2: Accessing gdb with the Eclipse CDT and MI CDT Debug Tutorial

    gdb -tui works okay if you want something GUI-ish, but still character based.

    Check out Nemiver C/C++ Debugger. It is easy to install in Ubuntu (Developer Tools/Debugging).

    Update: New link.

    I loathe the idea of Windows development, but the VC++ debugger is among the best I've seen. I haven't found a GUI front end that comes close to the VC one.

    GDB is awesome once you really get used to it. Use it in anger enough and you'll become very proficient. I can whiz around a program doing all the things you listed without much effort anymore. It did take a month or so of suffering over a SSH link to a remote server before I was proficient. I'd never go back though.

    DDD is really powerful but it was quite buggy. I found it froze up quite often when it got messages from GDB that it didn't grok. It's good because it has a gdb interface window so you can see what's going on and also interact with gdb directly. DDD can't be used on a remote X session in my environment (a real problem, since I'm sitting at a thin client when I do Unix dev) for some reason so it's out for me.

    KDevelop followed typical KDE style and exposed EVERYTHING to the user. I also never had any luck debugging non KDevelop programs in KDevelop.

    The Gnat Programming Studio (GPS) is actually quite a good front-end to GDB. It doesn't just manage Ada projects, so it's worth trying out if you are in need of a debugger.

    You could use Eclipse, but it's pretty heavy weight and a lot of seasoned Unix people I've worked with (me included) don't care much for its interface, which won't just STFU and get out of your way. Eclipse also seems to take up a lot of space and run like a dog.

    Qt Creator seems like good stuff. A colleague showed me one way set it up for debugging:

    Create a new project, "Import of Makefile-based Project". Point it to your root project folder (it will index sources under it, and it is impressively fast). Go to project settings and add a run configuration, then specify the executable you want to debug, and its arguments. Qt Creator seems to insist on building your project before debugging it. If you don't want that, or don't use make, you can override the make command. I changed it to "true". 🙂

    That may seem like a bit much work for debugging an app I had already compiled, but it is worth it. The debugger shows threads, stacks and local variables in a similar way to Visual Studio and even uses many of the same keyboard shortcuts. It seems to handle templates well, at least std::string and std::map. Attaching to existing processes and core dumps seems to be supported, though I haven't tested it yet.

    Keep in mind that I used it for less than and hour now, but I'm impressed so far.

    I use cgdb, simple and usefull

    I use DDD a lot, and it's pretty powerful once you learn to use it. One thing I would say is don't use it over X over the WAN because it seems to do a lot of unnecessary screen updates.

    Also, if you're not mated to GDB and don't mind ponying up a little cash, then I would try TotalView. It has a bit of a steep learning curve (it could definitely be more intuitive), but it's the best C++ debugger I've ever used on any platform and can be extended to introspect your objects in custom ways (thus allowing you to view an STL list as an actual list of objects, and not a bunch of confusing internal data members, etc.)

    Check out the Eclipse CDT project. It is a plugin for Eclipse geared towards C/C++ development and includes a fairly feature rich debugging perspective (that behind the scenes uses GDB). It is available on a wide variety of platforms.

    Johannes Schaub - litb

    Similar comfortable to the eclipse gdb frontend is the emacs frontend, tightly tied to the emacs IDE. If you already work with emacs, you will like it:

    GDB Emacs Frontend

    I used KDbg (only works under KDE).

    DDD is the GNU frontend for gdb: http://www.gnu.org/software/ddd/

    I've tried a couple of different guis for gdb and have found DDD to be the better of them. And while I can't comment on other, non-gdb offerings for linux I've used a number of other debuggers on other platforms.

    gdb does the majority of the things that you have in your wish list. DDD puts a nicer front on them. For example thread switching is made simpler. Setting breakpoints is as simple as you would expect.

    You also get a cli window in case there is something obscure that you want to do.

    The one feature of DDD that stands out above any other debugger that I've used is the data "graphing". This allows you to display and arrange structures, objects and memory as draggable boxes. Double clicking a pointer will open up the dereferenced data with visual links back to the parent.

    Qt Creator-on-Linux is certainly on par with Visual Studio-on-Windows for C++ nowadays. I'd even say better on the debugger side.

    There's one IDE that is missing in this list and which is very efficient (I've used it in many C/C++ projects without any issues): Netbeans.

    What can be stepped through is going to be limited by the debugging information that g++ produces, to a large extent. Emacs provides an interface to gdb that lets you control it via the toolbars/menus and display data in separate windows, as well as type gdb commands directly. Eclipse's CDT provides similar tools. I've heard of Anjuta and Code::Blocks but never used them.

    As someone familiar with Visual Studio, I've looked at several open source IDE's to replace it, and KDevelop comes the closest IMO to being something that a Visual C++ person can just sit down and start using. When you run the project in debugging mode, it uses gdb but kdevelop pretty much handles the whole thing so that you don't have to know it's gdb; you're just single stepping or assigning watches to variables.

    It still isn't as good as the Visual Studio Debugger, unfortunately.

    You don't mention whether you are using Windows or UNIX.

    On UNIX systems, KDevelop is good but I use KDbg because it is easy to use and will also work with apps not developed in KDevelop.

    Eclipse is good on both platforms.

    On Windows, there is a great package called Wascana Desktop Developer which is Eclipse CDT and MinGW all packaged up and preconfigured nicely for the minimum of pain. Its the best thing I've found for developing GNU code on Windows.

    I have used all these debuggers and none of them are as good as MS Dev Studio. Eclipse/Wascana is probably the closest but it does have limitations like you cannot step into DLLs and it doesn't do as good a job at examining variables.

    Viktor Malyi

    Have you ever taken a look at DS-5 debugger?

    There is a paid version which includes a lot of helpful features, but you can also use Community Edition for free (which is also quite useful especially for embedded systems).

    I have a positive experience with this tool when debugging Android applications on real device using eclipse.

    The Code:Blocks C++ IDE has a graphical wrapper, with a few of the features you want, but nothing like the power of VS.

    VisualGDB is another Visual Studio plugin to develop and debug applications on linux and embedded platforms.

    KDevelop works pretty well.

    Have you tried gdb -w with cygwin gdb. It is supossed to have a windows interface which works fairly well.

    The only problem I found is that on my present machine it didn't run that way until after I installed ddd. I suspect that it requires tcltk which was installed when I installed ddd.

    Latest version of Geany supports it (only on Linux, though)

    If you are looking for gdb under Visual Studio, then check WinGDB.

    In the last 15 months I use insight (came with FC6). It is not great, it is written in Tcl/Tk, but it is simple and useful. DDD is of similar quality / utility, but somewhat harder to use (various GUI gotchas and omissions). I also tried to integrate gdb with my IDE, SlickEdit. It worked OK (I played some 4 hours with it), but I did not like the GUI context switches. I like my IDE to remain unchanged while I am debugging; on Windows I use SlickEdit for IDE and Visual Studio Debugger for debugging. So from the 3: Insight, DDD and SlickEdit, Insight is my 1st choice, I use it >95% of the time, command-line gdb and DDD make up the other 5%. If I get the chance, I will eval Eclipse at some point, my work PC does not seem to have enough RAM (1GB only) to run Eclipse reasonably well.

    I have also heard a lot of praise for TotalView, including 1st hand during a job interview. I obtained an eval for our company in late 2008, but in the end we did not proceed as gdb was good enough for our needs; and it is free and ubiquitous.

    Use www.zero-bugs.com/ Zero debugger, it requires C++0x support from gcc

    I was searching for a debugger to step through a running programm. Say: Attach. The programm was build with eclipse, but because of maybe some multithreadding obstrucles, no sourcefiles where fond. What ever.

    I got very compfortable with NetBeans.

    [debug] from menu -> Attach Deugger... as process chose the one to debug as project [new project]

    Now the window disappars and you see nothing. detach from the process. The Read Square "Stop" helps.

    import source from the project as e.g. folder. ".../MyProject/src Now it appears in your project, and you can set breakpoints. again ttach debugger chose the process to debug. debugger should stop if programm reaches next breakpoint.

    Going to [window] -> [Debugging] -> Will your window make compfortable.

    来源:https://stackoverflow.com/questions/79023/is-there-a-c-gdb-gui-for-linux

    read more
  • 在Linux服务器上部署SpringBoot项目:

    1.首先将SpringBoot项目打包成JAR包,通过xFTP或者其他工具将JAR包上传到Linux上,然后执行如下命令启动项目:

    java -jar xxx.jar &

    该命令启动jar,一旦Xshell窗口关闭,JAR就停止运行了.

    如果想让项目在后台一直运行,通过如下命令启动JAR:

    nohup java -jar xxx.jar & >> consoleMsg.log 2>&1 &

    注意:consoleMsg.log文件要先创建,执行命令:

    touch consoleMsg.log

    查看项目运行日志:

    1、tailf mywork.log | grep --line-buffered findUserList

    实时跟踪日志,这里是只要findUserList 这个方法被运行,就会将它的日志打印出来,用于跟踪特定的日志运行。

    --line-buffered 是一行的缓冲区,只要这一行的缓冲区满了就会打印出来,所以可以用于实时监控日志。

    2、 tailf -n 500 mywork.log 打印最后500行日志,并且持续跟踪日志。

    tail -n 2000 mywork.log | more 分页查看最后2000行日志。

    来源:https://www.cnblogs.com/MrEcho/p/11370411.html

    read more

最新主题

301
Online

339
Users

226.6k
Topics

380.4k
Posts

在线用户

推荐阅读

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