【问题标题】:Check If Location Value Is Present In Array检查位置值是否存在于数组中
【发布时间】:2020-08-02 11:04:44
【问题描述】:

我正在编写一个脚本来解析 LinkedIn-CV。我被困在工作经验部分。目前我能够从 PDF 中提取工作经验文本。但我对位置键有疑问,因为它是可选的。

Array
(
    [0] => Company 1
    [1] => Software Engineer
    [2] => July 2020 - Present  (1 month)   
    [3] => Pretoria, Gauteng, South Africa //this key is optional
    [4] => Company 2
    [5] => CTO
    [6] => September 2016 - Present     (3 years 11 months) 
    [7] => Pretoria, South Africa //this key is optional
)

格式如下:

  • 公司名称 - 必填
  • 职位 - 必修
  • 工作时间 - 必修
  • 位置 - 可选

我尝试使用 array_chunk($array, 4); 但这仅在该位置存在于数组中时才有效。

我的另一个尝试是在整个数组中搜索某个国家/地区的存在,但这很棘手,因为某些公司的名称包含国家/地区。像 MTN - 南非。

我最后一次尝试是尝试编写一个正则表达式来检查位置模式。 LinkedIn 将其解析为 City, Province, Country 代表南非。但对于其他国家/地区,它解析为City, Country。但我无法正确理解这一点。我试过preg_match('#\((,*?)\)#', $value, $match),其中$value是当前迭代的字符串值

我想为每个工作经验创建一个数组,可以包含位置,也可以不包含。例如:

Array
(
    [0] => Array
        (
            [0] => Company 1
            [1] => Software Engineer
            [2] => July 2020 - Present  (1 month)   
            [3] => Pretoria, Gauteng, South Africa
        )

    [1] => Array
        (
            [0] => Company 2
            [1] => CTO
            [2] => September 2016 - Present     (3 years 11 months) 
            [3] => Pretoria Area, South Africa
        )

)

感谢您的帮助。

编辑:

主串(工作经历)

$string = 'Company 1 Software Engineer July 2020 - Present  (1 month) Pretoria, Gauteng, South Africa Company 2 CTO September 2016 - Present  (3 years 11 months) Pretoria Area, South Africa';

$array = splitNewLine($string);

function splitNewLine($text) {
    $code = preg_replace('/\n$/', '', preg_replace('/^\n/', '', preg_replace('/[\r\n]+/', "\n", $text)));
    return explode("\n", $code);
}

【问题讨论】:

  • 真的没有办法用关联数组代替吗?
  • @Viney 是的,这是可能的。但我很困惑。这有什么帮助?当我从字符串转换为数组时。你需要我包含主字符串吗?
  • 是的,了解您创建这些数组的准确程度会很有帮助
  • “持续时间”字段的值是否总是以月份开头?
  • @AndrewHardiman 是的,他们这样做了

标签: php regex preg-match linkedin pdf-parsing


【解决方案1】:

您可以一次抓取第 4 行,然后使用适当的正则表达式检查位置,然后相应地调整下一个块的位置:

function computeExperiences(array $lines): array
{
  $experiences = [];

  $position = 0;
  while ($chunkLines = array_slice($lines, $position, 4)) {
    $experience = array_slice($chunkLines, 0, 3);
    $locationIsPresent = isset($chunkLines[3]) && preg_match('/\w+,\s\w+(?:,\s\w+)?/', $chunkLines[3]);
    if ($locationIsPresent) {
      $experience[] = $chunkLines[3];
      $position += 4;
    } else {
      $position += 3;
    }
    $experiences[] = $experience;
  }

  return $experiences;
}

Demo

【讨论】:

  • 嗨@Jeto 感谢您的解决方案。如何将我使用的数组格式(数字)转换为您使用的格式?
  • 哦不,我不是这个意思。您工作的初始版本。唯一的问题是您的代码只返回了一种工作经验,而不是全部。我猜是因为我的$lines 是一个数值数组
  • @Rotimi 它确实返回了所有经验。我使用了您的确切格式,您可以查看示例下方的演示链接。
  • 嗨@Jeto 这是我的问题Array ( [0] => Company 1 [1] => Software Engineer [2] => July 2020 - Present (1 month) [3] => Pretoria, Gauteng, South Africa //this key is optional [4] => Company 2 [5] => CTO [6] => September 2016 - Present (3 years 11 months) [7] => Pretoria, South Africa //this key is optional ) => 数字数组的格式
  • 哦,是的,很好。整个简历解析对我来说是一个新领域。感谢您的帮助和耐心!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多