sourcetip

PHP에서 따옴표를 빼는 중

fileupload 2023. 9. 10. 12:29
반응형

PHP에서 따옴표를 빼는 중

구문 오류가 발생하는데, 따옴표 때문인 것 같습니다."time". 어떻게 하면 전체 끈으로 취급할 수 있을까요?

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

문제는 수동으로 추가할 수 없다는 것입니다.\따옴표 앞에이것이 제가 비교해야 할 실제 텍스트입니다.

백슬래시를 다음과 같이 사용합니다.

"From time to \"time\"";

백슬래시는 PHP에서 따옴표 안의 특수 문자를 피하기 위해 사용됩니다.PHP는 문자열과 문자를 구분하지 않기 때문에 이것을 사용할 수도 있습니다.

'From time to "time"';

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

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

당신의 질문에 대한 마지막 편집에 따르면, 이 점을 위해 당신이 할 수 있는 가장 쉬운 일은 'herdoc'을 사용하는 것이라고 생각합니다.일반적으로 사용되지 않으며 솔직히 권장하지는 않지만 이 텍스트 벽을 하나의 문자열로 빠른 방법을 원한다면 사용할 수 있습니다.구문은 여기 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;

addslash 기능을 사용합니다.

 $str = "Is your name O'Reilly?";

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

html specialchars()를 사용합니다.그러면 따옴표와 기호보다 작은 / 큰 것은 HTML 태그를 깨트리지 않습니다~

텍스트를 PHP 파일이 아닌 "text.txt"라는 일반 텍스트 파일에 저장합니다.

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

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

아니면

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

인용구를 피하거나:

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

또는 작은 따옴표를 사용하여 문자열을 표시합니다.

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

PHP 함수 addslash()를 사용하여 문자열을 호환할 수 있습니다.

언급URL : https://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php

반응형