PHP如何从文件中删除字符串
我有一个名为PHP如何从文件中删除字符串,php,string,str-replace,Php,String,Str Replace,我有一个名为test.txt的文件,我想删除该文件中长度小于30个字符的行,并且不应删除以大写字母单词开头、以点或问号结尾的行 例如,test.txt文件的内容是: text 1 text 2 text 3 Long text. text 4 Long text 2? 文本1 文本2 文本3 长文本。 文本4 长文本2? 过滤后,结果应为 Long text. Long text 2? 长文本。 长文本2? 下面是一个示例代码,可以实现这一点 test.txt内容: text 1 text 2
test.txt
的文件,我想删除该文件中长度小于30个字符的行,并且不应删除以大写字母单词开头、以点或问号结尾的行
例如,test.txt文件的内容是:
text 1
text 2
text 3
Long text.
text 4
Long text 2?
文本1
文本2
文本3
长文本。
文本4
长文本2?
过滤后,结果应为
Long text.
Long text 2?
长文本。
长文本2?
下面是一个示例代码,可以实现这一点
test.txt内容:
text 1
text 2
Long text.
text3
Long text 2?
Line with 30 characters ending with a question mark?
text4
文本1
文本2
长文本。
文本3
长文本2?
以问号结尾的30个字符的行?
文本4
执行代码后输出
Line with 30 characters ending with a question mark?
以问号结尾的30个字符的行?
希望它能帮助您完整的程序代码。它给了我一个空文件。这里应该只写30个字符以上的句子,开头用大写字母,结尾用问号或圆点
<?php
# create and load the HTML
include('simple_html_dom.php');
$tekst = file_get_html('http://www.naszawiedza.pl/')->plaintext;
foreach ($tekst as $key=>&$value) {
if (strlen($value) > 60) {
unset($yourArray[$key]);
}
}
echo $tekst;
//kropka
$string = $tekst;
$substr = '.';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//znak zapytania
$string = $tekst;
$substr = '?';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//podwójna spacja
$string = $tekst;
$substr = '\r\n\r\n';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//Wykrzyknik
$string = $tekst;
$substr = '!';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//tabulator
$string = $tekst;
$substr = ' ';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
echo $newstring;
// zmienna $dane, która będzie zapisana
// może także pochodzić z formularza np. $dane = $_POST['dane'];
$dane = $newstring;
// przypisanie zmniennej $file nazwy pliku
$file = "testy.txt";
// uchwyt pliku, otwarcie do dopisania
$fp = fopen($file, "a");
// blokada pliku do zapisu
flock($fp, 2);
// zapisanie danych do pliku
fwrite($fp, $dane);
// odblokowanie pliku
flock($fp, 3);
// zamknięcie pliku
fclose($fp);
//usun puste wiersze
$plik = "testy.txt";
// odczyt
$bufor = array();
$fd = fopen($plik, "r");
while (!feof ($fd))
{
$linia = fgets($fd, 1024);
if(strlen(trim($linia)))
{
$bufor[] = $linia;
}
}
fclose($fd);
// zapis
$fdw = fopen($plik, "w");
foreach($bufor as $wiersz)
{
fwrite($fdw, $wiersz);
}
fclose($fdw);
$file = fopen("testy.txt", "r");
$i = 0;
$string = "";
while(!feof($file))
{
// get the line
$line = trim(fgets($file));
// check if line have 30 characters
if(strlen($line) > 30)
{
// get first character ascai value
$value = ord(substr($line, 0, 1));
// get the last character
$last = substr($line, -1);
// now check if it has allowed criteria
if((($value >= 65 && $value <= 90) && ($last == '.' || $last == '?')))
{
$string .= $line."\n";
}
}
}
fclose($file);
// put the proccessed content back to file
file_put_contents("testy.txt", trim($string));
?>
请提供更多详细信息,说明您到底想做什么,以及您为此做了哪些尝试?**如何从文件中删除短于例如20个符号的行。**这条线是什么意思?用适当的细节和例子更新你的问题好吧,你试过什么?也许你应该先读一下。无论如何,你的问题可以分为三个任务:1)阅读文件。2) 处理内容(过滤器)。3) 将处理过的内容写入文件。关于1和3有很多信息,所以你应该能够自己解决。因此,我认为你的问题在于第二步。我建议您向我们展示处理问题的代码,并编辑您的问题,以包含与问题代码相关的特定问题。您刚刚编辑了问题,但仍然缺少重要信息。首先,您得到的输出是什么?还是根本不起作用?如果是,会发生什么?错误消息?因此,你真正的问题是什么?另外,旁注:我建议用英语编码。在(国际)团队中工作或在国际社会中寻求问题时(如现在),它非常有用。:)我得到这样的输出:文本1文本2文本3长文本。文本4长文本2?它应该过滤少于30个符号,并且必须以逗号或问号结尾。真的吗?你试过了吗?我试过这个代码,效果很好,很好开始使用,我的单词更长,所以我的单词长度超过30个符号,没有点和questoin标记。如果u'zpou可以检查这是我如何使用第二个答案中的代码什么?它将给出超过30个字符的u行,或以大写字母开头并以任意一个字符结尾的u行。或我无法理解你到底需要什么。
Line with 30 characters ending with a question mark?
<?php
# create and load the HTML
include('simple_html_dom.php');
$tekst = file_get_html('http://www.naszawiedza.pl/')->plaintext;
foreach ($tekst as $key=>&$value) {
if (strlen($value) > 60) {
unset($yourArray[$key]);
}
}
echo $tekst;
//kropka
$string = $tekst;
$substr = '.';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//znak zapytania
$string = $tekst;
$substr = '?';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//podwójna spacja
$string = $tekst;
$substr = '\r\n\r\n';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//Wykrzyknik
$string = $tekst;
$substr = '!';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
//tabulator
$string = $tekst;
$substr = ' ';
$attachment = "\r\n";
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
echo $newstring;
// zmienna $dane, która będzie zapisana
// może także pochodzić z formularza np. $dane = $_POST['dane'];
$dane = $newstring;
// przypisanie zmniennej $file nazwy pliku
$file = "testy.txt";
// uchwyt pliku, otwarcie do dopisania
$fp = fopen($file, "a");
// blokada pliku do zapisu
flock($fp, 2);
// zapisanie danych do pliku
fwrite($fp, $dane);
// odblokowanie pliku
flock($fp, 3);
// zamknięcie pliku
fclose($fp);
//usun puste wiersze
$plik = "testy.txt";
// odczyt
$bufor = array();
$fd = fopen($plik, "r");
while (!feof ($fd))
{
$linia = fgets($fd, 1024);
if(strlen(trim($linia)))
{
$bufor[] = $linia;
}
}
fclose($fd);
// zapis
$fdw = fopen($plik, "w");
foreach($bufor as $wiersz)
{
fwrite($fdw, $wiersz);
}
fclose($fdw);
$file = fopen("testy.txt", "r");
$i = 0;
$string = "";
while(!feof($file))
{
// get the line
$line = trim(fgets($file));
// check if line have 30 characters
if(strlen($line) > 30)
{
// get first character ascai value
$value = ord(substr($line, 0, 1));
// get the last character
$last = substr($line, -1);
// now check if it has allowed criteria
if((($value >= 65 && $value <= 90) && ($last == '.' || $last == '?')))
{
$string .= $line."\n";
}
}
}
fclose($file);
// put the proccessed content back to file
file_put_contents("testy.txt", trim($string));
?>