복붙노트

PHP에서 이스케이프 인용 부호

PHP

PHP에서 이스케이프 인용 부호

구문 분석 오류가 발생했습니다. "시간"보다 큰 따옴표 때문에 생각합니다. 어떻게 그것을 전체 문자열로 처리 할 수 ​​있습니까?

<?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%";

문제는 모든 인용 부호 앞에 수동으로 \를 추가 할 수 없다는 것입니다. 이것은 필자가 비교해야 할 실제 텍스트입니다.

해결법

  1. ==============================

    1.그러한 백 슬래시를 사용하십시오.

    그러한 백 슬래시를 사용하십시오.

    "From time to \"time\"";
    

    백 슬래시는 따옴표 안에있는 특수 문자를 이스케이프하기 위해 PHP에서 사용됩니다. PHP는 문자열과 문자를 구별하지 않으므로 이것을 사용할 수도 있습니다.

    'From time to "time"';
    

    작은 따옴표와 큰 따옴표의 차이점은 큰 따옴표는 문자열 보간을 허용한다는 것입니다. 즉, 문자열에서 변수를 인라인으로 참조 할 수 있으며 해당 값은 문자열에서 평가됩니다

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

    귀하의 질문에 대한 귀하의 마지막 편집마다이 시점에서 할 수있는 가장 쉬운 일은 'heredoc'을 사용하는 것입니다. 그들은 일반적으로 사용되지 않으며 정직하게 나는 일반적으로 권장하지 않지만이 벽을 단일 문자열로 빠르게 가져 오는 방법을 원한다면 권장합니다. 구문은 다음에서 찾을 수 있습니다. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 여기에 예제가 있습니다.

    $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;
    
  2. ==============================

    2.addslashes 함수를 사용하십시오.

    addslashes 함수를 사용하십시오.

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

    3.

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

    또는

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

    4.텍스트를 php 파일이 아닌 "text.txt"라는 일반 텍스트 파일에 저장하십시오.

    텍스트를 php 파일이 아닌 "text.txt"라는 일반 텍스트 파일에 저장하십시오.

    하나의 간단한 $ text1 = file_get_contents ( 'text.txt'); 명령에는 한 가지 문제가없는 텍스트가 있습니다.

  5. ==============================

    5.모든 문자열에 addslashes () PHP 함수를 사용하여 호환 가능하도록 만들 수 있습니다.

    모든 문자열에 addslashes () PHP 함수를 사용하여 호환 가능하도록 만들 수 있습니다.

    http://php.net/manual/en/function.addslashes.php

  6. ==============================

    6.인용 부호를 이스케이프합니다.

    인용 부호를 이스케이프합니다.

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

    작은 따옴표를 사용하여 문자열을 나타낼 수 있습니다.

    $text1= 'From time to "time"';
    
  7. from https://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php by cc-by-sa and MIT license