我有一个示例字符串,如下所示。在字符串中,如何删除以(以及删除行开头)结尾的行并获取每行的第二个字?
This is some text (
some more text,
one line text,
more and more text available,
this one is even longer and even more text is available,
) this is the last line;
This is also some more text (
Second time some more text,
one line text second time ,
Once again more and more text available,
finally this one is even longer and even more text is available,
) this is the last line;
上面显示的字符串来自文本文件。可以粘贴在textarea中,也可以上传文件,并从文件中读取该文本。因此,在上面显示的示例中,预期输出为:
some more text,
one line text,
more and more text available,
this one is even longer and even more text is available,
Second time some more text,
one line text second time ,
Once again more and more text available,
finally this one is even longer and even more text is available,
//words are
more
line
and
one
time
line
again
this
如何在PHP中执行上述操作?
答案 0 :(得分:1)
您可以使用explode
,array_map
和array_filter
功能来执行您想要的操作。
首先,你必须定义函数来过滤掉(和)行,并提取每行的第二个单词。
function filter_line($line) {
//the regular expression detects a line ending with a ( or beginning with a )
if(preg_match('/(^\)|\($)/', trim($line))) return false;
if(empty($line)) return false;
return true;
}
function map_line($line) {
//the regular expression here splits the line into pieces at any whitespace
//in case they used multiple spaces or a tab
$split = preg_split('/\s+/', trim($line));
//if there are one or zero words on this line then remove it from the result
if(count($split) < 2) return false;
//return the second word
return $split[1];
}
现在你已经拥有了,其余的很容易:
//this will split the whole text into an array of lines
$array = explode("\n", $myTextGoesHere);
//this will return an array of entries that are either the second word or false
$filtered = array_filter($array, 'filter_line');
//at this point $filtered contains the first step.
print_r($filtered);
//finally, this will extract the second word from each line
//and eliminate lines with less than two words
$result = array_filter(array_map('map_line', $filtered));
//$result contains the output
print_r($result);
全部完成。
顺便说一句,很明显,您正在尝试从CREATE TABLE
语句中获取类型值 - 您可能需要更加小心地根据您的RDBMS来解析它。
答案 1 :(得分:1)
//Assumes text is in $text
//Some preparations
$lines=explode("\n",$text);
$output=array();
$words=array();
$recording=false;
//Cycle lines
foreach ($lines as $line) {
//Empty lines: Keep
if ($line=='') {
$output[]='';
continue;
}
//Not recording: Wait for '('
if (!$recording) {
if (substr($line,-1)=='(') $recording=true;
continue;
}
//Recording: Ending?
if (substr($line,0,1)==')') {
$recording=false;
continue;
}
//Recording: Keep line
$output[]=$line;
//Recording: Keep 2nd word
$line=preg_split('/\s+/', trim($line));
if (sizeof($line)>1) $words[]=$line[1];
//Remove next line if you want to ignore 1-word lines
else $words[]='--no-second-word--';
}