PHP:删除以结尾的行(删除以开头的行),并抓取每行的第二个单词

PHP:删除以结尾的行(删除以开头的行),并抓取每行的第二个单词,php,Php,我有一个示例字符串,如下所示。在字符串中,如何删除以结尾的行(以及删除以开头的行),并获取每行的第二个单词 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 ( Se

我有一个示例字符串,如下所示。在字符串中,如何删除以结尾的行(以及删除以开头的行),并获取每行的第二个单词

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;
上面显示的字符串来自文本文件。可以粘贴到文本区域,也可以上载文件,并从文件中读取此文本。因此,在上述示例中,预期输出为:

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中执行上述操作?

您可以使用
分解
数组映射
数组过滤器
函数来执行您想要的操作

首先,您必须定义函数以过滤掉(和)行,并提取每行的第二个字

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];
}
都做完了


顺便说一下,很明显,您正试图从
CREATE TABLE
语句中获取类型值-您可能需要更加小心地根据您的RDBMS解析该类型值。

您是否做过任何研究,或者只是希望得到完整的解决方案?这在现实世界中的应用是什么?请查看
preg\u replace
函数<代码>$匹配行尾,
^
匹配行首
explode
可能有助于您找到每行的第二个单词。打印$output&$words显示为空白。是否缺少一些内容?只是将其粘贴到PHP中,工作正常-您是否按照第一条注释中的指示填充了$text?我的行为方式很奇怪。是的,我确实将字符串分配给了$text变量,还使用了print\r($output);打印(大写);它给了我这个:数组([0]=>)数组()+1。你说得对。它丢失了\n。当我添加它们时,效果很好。我已经接受了另一个答案作为解决方案&因为我不能接受同一个问题的两个解决方案,我投票支持你的答案。非常感谢。这确实给出了单词,但完全去掉了两段文字。我没有意识到你在每一步都需要输出。好的,我想我们需要打印($array);为了显示字符串,我修改了解决方案,以便您可以在每个步骤中获得输出。你知道发生了什么变化吗
array\u filter
还可以使用返回true或false的函数来包含行或删除行。首先运行此命令以删除不需要的行。然后,调用
array\u map
从每行中提取第二个单词,并再次调用
array\u filter
删除至少没有两个单词的任何行(这可能是必要的,也可能不是必要的,取决于输入的一致性)。非常感谢!现在我想我确实明白发生了什么。段落的行存储在数组中,但我可以检索它们并将其转换为字符串。没问题。非常感谢你的帮助!
//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--';

}
//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--';

}