【问题标题】:Saving processed POST data in a file using PHP使用 PHP 将处理后的 POST 数据保存在文件中
【发布时间】:2017-01-30 08:14:35
【问题描述】:

我正在开发一个 chrome 扩展,它将一些 HTML 内容发送到后端 PHP 服务器文件。使用 POST 发送和接收 HTML。我将收到的文本(HTML)保存到一个文件中,该文件将内容存储为:

       "<a href=""http://www.google.com/"">
           <img src=""http://www.google.com/logos/Logo_25wht.gif"" border=""0""  alt=""Google"" align=""middle""></a>"

我需要将文本(HTML)保存为:

<a href="http://www.google.com/"><img src="http://www.google.com/logos /Logo_25wht.gif" border="0" alt="Google" align="middle"></a>

注意,我需要这些东西: 1. 文本应该在一行中。 2. 去掉标签属性中重复的双引号(" ")。

我的 PHP Server 页面是这样的:

  if (isset($_POST['content']))
  {

    $url = trim($_POST['content']);

    //$url = str_replace(array("\n","\r"),'', $url);
    //$url=str_replace("&ampquot;", "'", $url);
    //$url = str_replace('""', '"', $url);

    $file = fopen("ad_file.csv","a");
    fwrite($file, $url)
    fclose($file); 


   }
?>

我也尝试了注释选项,但没有成功。

谢谢。

【问题讨论】:

  • 嗨@Shakoor Ab,要将任何内容存储为单行,您应该使用 str_replace() 并删除 \n 和 \r 字符。关于双引号,您可以这样做,但真诚地,我会先查看页面侧并找出发送的原因。使用什么控件来输入此信息? TextArea,任何类型的编辑器?
  • 我从 chrome 扩展程序接收此信息,我需要将其存储在如上所述的文件中。
  • @Charle ,我已经尝试过它们,就像在 cmets 中一样。但没有成功。

标签: php html file post


【解决方案1】:

我的解决办法是这样的:

$content = '"<a href=""http://www.google.com/"">
        <img src=""http://www.google.com/logos/Logo_25wht.gif"" border=""0""  alt=""Google"" align=""middle""></a>"';

echo processText($content);

function processText($text)
{
   $text = trim($text, '"');
   $text = str_replace(array("\n", "\r"), '', $text);
   $text = str_replace('""', '"', $text);
   $text = preg_replace('#\s{2,}#ims', ' ', $text);
   $text = str_replace('> <', '><', $text);

   return $text;
}

它应该回应这个:

<a href="http://www.google.com/"><img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></a>

在这里运行它:http://sandbox.onlinephpfunctions.com/code/14c0852bb945f70debad32d8cd5305ee650a3f59

【讨论】:

    【解决方案2】:

    ; 缺失:

    fwrite($file, $url)

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 2021-09-17
      • 2014-07-05
      • 1970-01-01
      相关资源
      最近更新 更多