我有一个名为test.txt
的文件,我想删除此文件中长度小于30个字符的行,并且以大写字母开头并以点或问号结尾的行不应删除
例如test.txt文件的内容是:
text 1 text 2 text 3 Long text. text 4 Long text 2?
过滤后,结果应为
Long text. Long text 2?
<?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);
答案 0 :(得分:0)
以下是一个可以执行此操作的示例代码
test.txt内容:
text 1 text 2 Long text. text3 Long text 2? Line with 30 characters ending with a question mark? text4
<?php
$file = fopen("test.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("test.txt", trim($string));
?>
执行代码后输出
Line with 30 characters ending with a question mark?
希望它会帮助你
答案 1 :(得分:0)
完整的程序代码。它给了我空文件。这里应该只有超过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));
?>