我有一个简单的PHP变量:
$Title = $_POST['text'];当它有这样的输入时:here's the text
当它写入一个文件时,PHP会识别为:here\'s the text
$Filename = 'index.php';
$Handle = fopen($Filename, "r");
$Contents = fread($Handle, filesize($Filename));
fclose($Handle);
$TitleTag = extractor($Contents, "<title>", "</title>");
$Contents = str_replace ($TitleTag, $Title, $Contents);
$Handle = fopen($Filename, 'w+');
fwrite($Handle, $Contents);
fclose($Handle);怎样才能准确地得到用户输入的内容?谢谢
发布于 2012-11-06 20:55:29
快速而简单的解决方法是:
fwrite($handle,stripslashes($Contents));see the docs
或者,您可以让php将内容视为字符串格式,这样也会正确地返回转义字符:
fwrite($Handle,sprintf($Contents));但是请注意,如果你的代码中有一个%字符,它将不得不加倍,如果没有,sprintf会把它当作一个占位符:
fwrite($Handle,sprintf(str_replace('%','%%',$Contents)));因此,这需要两个操作,并且不是最好的方法,特别是因为我感觉您正在处理一个HTML (style='width:50%' <--并非完全不可能)
https://stackoverflow.com/questions/13251461
复制相似问题