2013-06-17 1 views
0

"Text.csv"문자열에 '내 이름은 "Robert" , Text.csv에 추가 된 문자열은 다음과 같습니다. my name is/"Robert /".CSV 파일을 읽을 때 htmlspecialchars()를 사용하여 특수 문자를 도용하지 않고 볼 수 있습니다.

내 코드에서 csv 파일에 쓰기 전에 $ _POST var이 PHP 함수 htmlspecialchars()로 이스케이프되고 있습니다. 그러나 여전히 슬래시가 보이고 있습니다.

무엇이 문제 일 수 있습니까? Text.csv 섹션에서 읽기

:
$myFile = "Text.csv"; 
$fh = fopen($myFile, 'r') or die("can't open file"); 
for ($i=0 ; $i<5 ; $i++) 
{ 
    $line=fgets($fh); 

    if ($line) 
    { 
    $line_of_text = explode(",", $line); 
echo htmlspecialchars($line_of_text[0]).'<br />'; 
echo htmlspecialchars($line_of_text[1]).'<br />'; 
echo htmlspecialchars($line_of_text[2]).'<br />'; 
echo htmlspecialchars($line_of_text[3]).'<br />'; 
echo htmlspecialchars($line_of_text[4]).'<br />'; 
} } fclose($fh); 

이 Text.csv 섹션에 쓰기는 :

header('Content-type: text/html; charset=UTF-8'); 
$myFile = "Text.csv"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
for ($i=1 ; $i<26 ; $i++) 
{ 
$str='l'.intval(($i-1)/5).'f'.(($i-1)%5); 

if (($i%5)==0) 
    { 
    $stringData = $_POST[$str].PHP_EOL; 
    fwrite($fh, htmlspecialchars($stringData)); 
    } 
Else 
    { 
    $stringData = $_POST[$str].","; 
    fwrite($fh, $stringData); 
    } 
} 

fclose($fh); 

답변

0

난 당신이 HTML 문자를 인코딩하는 htmlentities()를 사용하여 디코딩 사용 html_entity_decode()을 위해한다고 생각합니다.

이 코드를 사용해보십시오. Text.csv 섹션에 쓰기

$myFile = "Text.csv"; 
$fh = fopen($myFile, 'r') or die("can't open file"); 
for ($i=0 ; $i<5 ; $i++) 
{ 
    $line=fgets($fh); 

    if ($line) 
    { 
    $line_of_text = explode(",", $line); 
    echo html_entity_decode($line_of_text[0]).'<br />'; 
    echo html_entity_decode($line_of_text[1]).'<br />'; 
    echo html_entity_decode($line_of_text[2]).'<br />'; 
    echo html_entity_decode($line_of_text[3]).'<br />'; 
    echo html_entity_decode($line_of_text[4]).'<br />'; 
} 
} fclose($fh); 

: Text.csv 섹션에서 읽기

header('Content-type: text/html; charset=UTF-8'); 
$myFile = "Text.csv"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
for ($i=1 ; $i<26 ; $i++) 
{ 
$str='l'.intval(($i-1)/5).'f'.(($i-1)%5); 

if (($i%5)==0) 
{ 
    $stringData = $_POST[$str].PHP_EOL; 
    fwrite($fh, htmlentities($stringData, ENT_QUOTES)); 
} 
else 
{ 
    $stringData = $_POST[$str].","; 
    fwrite($fh, htmlentities($stringData, ENT_QUOTES)); 
} 
} 

fclose($fh); 
+0

고맙습니다 시도에 대한했으나 제대로 동작하지 않습니다. 결과는 동일합니다 ... = \ –

관련 문제