我有一个文本区域和一个提交按钮,一旦我把东西写进文本区域并按下按钮,它就会把文本区域的内容写进一个txt文件,我需要帮助使用爆炸来格式化这些内容。以下是我用来将文本区域内容写入txt文件的代码:
$tavalues = ($_POST['dname']); //dname is textarea field
$filename = "imones.txt";
$fp = fopen ($filename, "w");
if ($fp){
fwrite($fp, $tavalues);
}
fclose($fp);
正如你所看到的,它写入imones.txt文件。现在我想读取该文件的内容,格式化它,并将格式化的内容写入另一个文件。我似乎不知道如何为爆炸写多个分隔符。。下面是一个我如何将数据输入到textarea的例子(都是用逗号之类的东西):
example.com example.com
example.com, example.com
example.com
以下是我希望它被格式化的方式(基本上我想删除',','''n','''r'):
example.com
example.com
example.com
(所有链接都在一行中,没有任何空格,请注意链接是不同的,而不是相同的)
也许可以试试这个。它又快又脏。若要获得更好的解决方案,应该尝试正则表达式!
$input = "exampleA.com exampleB.com
exampleC.com, exampleD.com
exampleE.com";
$tmp = explode(" ", $input);
$str = "";
$filename = "imones.txt";
$fp = fopen ($filename, "w");
if ($fp){
for ($i = 0; $i < count($tmp); $i++) {
if ($tmp[$i] != "") {
$tmp[$i] = str_replace(",", "", $tmp[$i]);
$tmp[$i] = trim($tmp[$i]);
$str .= $tmp[$i]."'n";
}
}
fwrite($fp, $str);
}
fclose($fp);
将所有正在爆炸的字符转换为一个并以此进行爆炸怎么样?!
$expl = ';';
$content = file_get_contents('imones.txt');
$content = str_replace(',', $expl, $content);
$content = str_replace(''n', $expl, $content);
$content = str_replace(''r', $expl, $content);
$content = str_replace(' ', $expl, $content);
// ...
while (strpos($content, "$expl$expl") !== false) { // while $expl is found twice
$content = str_replace("$expl$expl", $expl, $content); // remove this
}
$parts = explode($expl, $content);
// than join them
$formatted = implode("'n", $parts);
file_put_contents('somefile.txt', $formatted);
试试PHP的preg_replace()。