Php 与Perl';领带
在php中是否有一个与PERL相关的等价物?我想看看文件中是否有一个字符串(单个单词),其中每一行都是一个单词/字符串。如果是,我想删除该条目。这在Perl中很容易做到,但不确定如何在PHP中做到这一点。根据您读取文件的方式,将决定如何删除条目 使用:Php 与Perl';领带,php,Php,在php中是否有一个与PERL相关的等价物?我想看看文件中是否有一个字符串(单个单词),其中每一行都是一个单词/字符串。如果是,我想删除该条目。这在Perl中很容易做到,但不确定如何在PHP中做到这一点。根据您读取文件的方式,将决定如何删除条目 使用: $filtered = ""; $handle = fopen("/file.txt", "r"); if ($handle) { // Read file line-by-line while (($buffer = fget
$filtered = "";
$handle = fopen("/file.txt", "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, "replaceMe") === false)
$filtered .= $buffer;
}
}
fclose($handle);
filterArray($value){
return (strpos($value) === false);
}
// Read file into a string
$string = file_get_contents('input.txt');
$array = explode("\n", $string);
$filtered = array_filter($array, "filterArray");
function filterArray($value){
return (strpos($value) === false);
}
// Read file into array (each line as an element)
$array = file('input.txt', FILE_IGNORE_NEW_LINES);
$filtered = array_filter($array, "filterArray");
使用:
$filtered = "";
$handle = fopen("/file.txt", "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, "replaceMe") === false)
$filtered .= $buffer;
}
}
fclose($handle);
filterArray($value){
return (strpos($value) === false);
}
// Read file into a string
$string = file_get_contents('input.txt');
$array = explode("\n", $string);
$filtered = array_filter($array, "filterArray");
function filterArray($value){
return (strpos($value) === false);
}
// Read file into array (each line as an element)
$array = file('input.txt', FILE_IGNORE_NEW_LINES);
$filtered = array_filter($array, "filterArray");
使用:
$filtered = "";
$handle = fopen("/file.txt", "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, "replaceMe") === false)
$filtered .= $buffer;
}
}
fclose($handle);
filterArray($value){
return (strpos($value) === false);
}
// Read file into a string
$string = file_get_contents('input.txt');
$array = explode("\n", $string);
$filtered = array_filter($array, "filterArray");
function filterArray($value){
return (strpos($value) === false);
}
// Read file into array (each line as an element)
$array = file('input.txt', FILE_IGNORE_NEW_LINES);
$filtered = array_filter($array, "filterArray");
注意:每个方法都假设如果条目包含一个单词,您希望删除整个条目。根据您读取文件的方式,将确定您删除条目的方式 使用:
$filtered = "";
$handle = fopen("/file.txt", "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, "replaceMe") === false)
$filtered .= $buffer;
}
}
fclose($handle);
filterArray($value){
return (strpos($value) === false);
}
// Read file into a string
$string = file_get_contents('input.txt');
$array = explode("\n", $string);
$filtered = array_filter($array, "filterArray");
function filterArray($value){
return (strpos($value) === false);
}
// Read file into array (each line as an element)
$array = file('input.txt', FILE_IGNORE_NEW_LINES);
$filtered = array_filter($array, "filterArray");
使用:
$filtered = "";
$handle = fopen("/file.txt", "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, "replaceMe") === false)
$filtered .= $buffer;
}
}
fclose($handle);
filterArray($value){
return (strpos($value) === false);
}
// Read file into a string
$string = file_get_contents('input.txt');
$array = explode("\n", $string);
$filtered = array_filter($array, "filterArray");
function filterArray($value){
return (strpos($value) === false);
}
// Read file into array (each line as an element)
$array = file('input.txt', FILE_IGNORE_NEW_LINES);
$filtered = array_filter($array, "filterArray");
使用:
$filtered = "";
$handle = fopen("/file.txt", "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, "replaceMe") === false)
$filtered .= $buffer;
}
}
fclose($handle);
filterArray($value){
return (strpos($value) === false);
}
// Read file into a string
$string = file_get_contents('input.txt');
$array = explode("\n", $string);
$filtered = array_filter($array, "filterArray");
function filterArray($value){
return (strpos($value) === false);
}
// Read file into array (each line as an element)
$array = file('input.txt', FILE_IGNORE_NEW_LINES);
$filtered = array_filter($array, "filterArray");
注意:每个方法都假设如果包含单个单词,则要删除整个条目。如果包含单个单词,则要删除整个条目/行,还是从每个条目/行中删除单个单词?整行,但这仅仅是因为我每行只有一个单词/条目。如果它包含单个单词,是否要删除整个条目/行,或者从每个条目/行中删除单个单词?整行,但这仅仅是因为我每行只有一个单词/条目。在第一种方法中,我不一致--我将单词附加到字符串中。要附加到数组,请声明
$filtered=array()
,并使用$filtered[]
而不是$filtered.=
进行附加。在第一种方法中,我不一致--我将单词附加到字符串中。要附加到数组,请声明$filtered=array()
,并使用$filtered[]
而不是$filtered.=
进行附加。