繁体   English   中英

php正则表达式提取部分文本

[英]php regular expression extract part of text

我需要从数据库中提取,其中一列中的记录以这种方式组合:第一个字母(名字 1)。 姓 1,第一个字母(名字 2)。 姓 2,....

这是我如何尝试解决的一个例子......

     $text2= "T. Toth, M. A. Carlo de Miller, T. Stallone";
     $keywords = preg_split("/,/", "$text2");

     print_r($keywords);

    //I got a result in this way:

    //Array ( [0] => T. Toth [1] => M. A. Carlo de Miller [2] => T. Stallone ) 

    // I want a result of the form :

    //Array ( [0] => T [1] => Toth [2] => M. A. [3] => Carlo de Miller [4] => T  and    so on....

有人可以了解如何进行吗? 即使它可以在 MYSQL 中

另一种变体:

$text2= "T. Toth, M. A. Carlo de Miller, T. Stallone";
$result = array();
foreach (explode(",",$text2) as $row)
{
  $row = explode(".",$row);
  $last = array_pop($row);
  $result[] = join(".",$row).".";
  $result[] = $last;
}
print_r($result);

结果:

Array ( [0] => T. [1] => Toth [2] => M. A. [3] => Carlo de Miller [4] => T. [5] => Stallone )

preg_split可能不是正确的功能。 preg_match_all试试这个:

$text2= "T. Toth, M. A. Carlo de Miller, T. Stallone";
preg_match_all("/\w{2,}(?:\s\w{2,})*|\w\.(?:\s\w\.)*/i", $text2, $matches);
print_r($matches[0]);

这会选择名称和首字母缩写,同时省略前导/尾随空格。

第一个匹配全名: \\w{2,}(?:\\s\\w{2,})*

第二场比赛首字母: \\w\\.(?:\\s\\w\\.)*

结果是:

Array ( [0] => Array ( [0] => T. [1] => Toth [2] => M. A. [3] => Carlo de Miller [4] => T. [5] => Stallone ) )

我认为这个正则表达式应该或多或少地做你想要的:

/
  (?:^|,)           # Start of subject or comma
  \s*               # Optional white space
  ((?:[a-z]\.\s*)+) # At least one occurrence of alpha followed by dot
  \s*               # Consume trailing whitespace
/ix

当与PREG_SPLIT_NO_EMPTYPREG_SPLIT_DELIM_CAPTURE捕获标志结合使用时,此表达式将获得您想要的结果,唯一需要注意的是它还会捕获一些前导/尾随空格。 我看不出有什么方法可以避免这种情况,并且在使用结果时可以轻松修剪掉它。

$str = 'T. Toth, M. A. Carlo de Miller, T. Stallone';
$expr = '/(?:^|,)\s*((?:[a-z]\.\s*)+)\s*/i';
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;

$keywords = preg_split($expr, $str, -1, $flags);

print_r($keywords);

看到它工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM