【发布时间】:2017-11-21 16:28:54
【问题描述】:
我需要从一个 txt 文件中删除一个特定的字符串集。目前,代码以类似的方式将数据直接发布到文件中。但是,尝试删除以相同方式输入的相同数据是不允许的。在当前状态下,删除字符串的代码如下所示。
我们被不允许使用预构建的排序函数或使用 str_replace 或类似代码之类的函数。
这里是删除字符串的当前代码:
$blankReplace = "";
$found = false;
$fileData = file($fileInput,FILE_IGNORE_NEW_LINES);
for($i = 0; ($i < count($fileData)) && != $found; $i ++)
{
if($fullNameAndEmail == $fileData[$i])
{
$pos = $i;
$name = $fileData[$i];
$found = true;
}
}
if($found == true)
{
// Exit path. Go to for($j = 0) path
unset($fileData[$pos]);
shuffle($fileData);
}
else
{
//Error Msg
}
$writeToFile = fopen($inputFile,'w+');
for($j = 0;$j<count($fileData);$j++)
{
if(trim($fileData[$j]) != " ")
{
$rewrittenList = trim($fileData[$j])."\n";
fwrite($writeToFile, $rewrittenList);
}
}
代码在研究错误后在代码中输出错误T_IS_NOT_EQUAL。数据作为来自 file() 读取的直接数据进来,所以它应该可以工作。该错误当前指向for($i = 0; ($i < count($fileData)) && != $found; $i ++) 行,但也可能引用了代码中的类似事件。
数据输入格式:
firstname lastname emailaddress
如果提到的名称出现多个实例,我还需要能够处理,所以说我们有:
Matt Person emailaddr@email.com
Matt Person emailaddr@email.com
在与此类似的情况下,它将删除一个实例,而不是全部。
感谢您的帮助,提前感谢您的时间。
编辑:
Example input:
Matthew person person@email.com
John holton person@email.com
Person Name person@gmail.com
用户将输入一个人的姓名(按上述格式),这将导致删除一个人。假设他们输入表单:
$fullName = "Matthew person";
$emailAddr = "person@email.com";
The output will edit the data to put the data into a single line again
$fullNameAndEmail = $firstName." ".$lastName." ".$emailAddr;
代码的输出,在本例中将删除“Matthew person person@email.com”
所以文本文件中的输出会输出:
John holton person@email.com
Person Name person@gmail.com
编辑 2:当前状态下的代码
<!doctype HTML>
<html>
<meta charset="utf-8">
<head>
<title>Updating the guest book!</title>
</head>
<body>
<?php
$fileInput = "guestBook.txt";
$fileInputInData = file_get_contents($fileInput); // Gets data from file
$testBool = file_exists($fileInput);
$fullName = trim($_POST["fullName"]);
$emailAddr = trim($_POST["emailAddr"]);
$fileSize = filesize($fileInput);
if(!empty($fullName) and !empty($emailAddr))
{
if($testBool == 0)
{
echo "There was an issue with the file. Please have it checked.</br>";
}
else
{
echo "Made it into else path for testBool </br>";
if($fileSize > 0)
{ #code for truth
echo "Made it into filesize check. Filesize: $fileSize </br>";
$fullNameLen = strlen($fullName);
$emailAddrLen = strlen($emailAddr);
$fullNameArr = explode(" ", $fullName);
$firstName = trim($fullNameArr[0]);
$lastName = trim($fullNameArr[1]);
$fullNameToWrite =$firstName." ".$lastName;
$emailAddrCheck=substr_count($emailAddr, "@");
if ($emailAddrCheck == 1)
{
echo "Email address check passed</br>";
#email addr entered right path
$fullNameAndEmail =$fullNameToWrite." ".$emailAddr." has signed in.\n";
$inputFile = "guestBook.txt";
//$pos = strpos($writeToFile, $fullNameAndEmail);
//$writeToFileEx = explode("\n", $fileInputInData);
$blankReplace = "";
$str = $fileInputInData;
$find = $fullNameAndEmail;
$arr=explode("\n", $str);
Foreach($arr as $key => &$line)
{
If($line == $find)
{
Unset($arr[$key]);
shuffle($arr);
}
}
$writeToFile = fopen($inputFile,'w+');
$rewrittenList = trim($arr)."\n";
fwrite($writeToFile, $rewrittenList);
fclose($inputFile);
}
else {
echo "Email address check failed. Invalid email address entered. </br>
Line 55 occured.</br>";
#email addr entered wrong message
}
//asort(array) sorts array low to high (ascending)
//arsort(array) sorts array high to low (descending)
}
else
{
echo "Did not make it into filesize check. Filesize: $fileSize. Line 63 occured </br>";
}
}
}
else if (empty($fullName) or empty($emailAddr))
{
echo "Error! Line 23: One of the inputs was left empty. Line 69 occured </br>";
}
else
{
echo "Error! Line 23: Did not detect any values in either data area,</br>and program
did not go into first error. Line 73 occured </br>";
}
?>
<br>
</body>
</html>
【问题讨论】:
-
开始:
!= $found应该是您提到的for循环中的!$found。目前是语法错误 -
向我们展示输入和输出。除了函数的输入和预期输出之外,您已经解释了所有内容。什么是
$fullNameAndEmail -
你想重新发明 str_replace 吗? 3v4l.org/gpTEg#vhhvm-3185
-
$fullNameAndEmail 包含一个有序集,按照用户输入表单的输入中的“firstname lastname emailaddr”的顺序保存。它将其存储到一个文本文件中,这里使用条目从中删除,以从留言簿中删除一个人。 @Andreas 另外,我们的教授不会让我们使用 str_replace,这就是为什么我们必须做这个复杂的函数集来做同样的事情。
-
所以你想重新发明str_replace?见上面的链接。现在你因为不清楚这是一项有限制的任务而被否决了。