Php:从文本文件中删除字符串

时间:2017-11-21 16:28:55

标签: php string file

我需要从txt文件中删除特定的字符串集。目前,代码以类似的方式工作以将数据直接发布到文件。但是,尝试删除以相同方式输入的相同数据将不允许它。在它的当前状态下,代码看起来像是删除字符串。

我们 NOT 允许使用预建的排序函数使用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>

1 个答案:

答案 0 :(得分:0)

我认为你过于复杂了 我预先确定每一行并检查它是否匹配 如果是的话,我取消了该行。

在循环之后,我在新行上内爆,字符串恢复正常,但没有$ find。

$str = "Matt Person emailaddr@email.com
John doe doesoe@gmail
Matt Person emailaddr@email.com
Trump donald@whitehouse";

$find = "Matt Person emailaddr@email.com";

$arr=explode("\n", $str);

Foreach($arr as $key => &$line){

    If($line == $find){
        Unset($arr[$key]);
    }
}
Echo implode("\n", $arr);

https://3v4l.org/hmSr7