正则表达式需要提取特定值

时间:2014-12-26 19:17:52

标签: php regex preg-match

我正在努力确定要与 preg_match 一起使用的正则表达式,以返回以下字符串的数组:

key_people = Mark Zuckerberg
([[Chairman]] and [[CEO]])
[[Sheryl Sandberg]]
([[Chief operating officer|COO]])

我正在寻找的数组类似于以下内容:

array(2) {
["Chairman and CEO"]=> "Mark Zuckerberg"
["Chief operating officer"]=> "Sheryl Sandberg"
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

它并不完美,但这就是我所做的:

$string = "key_people = Mark Zuckerberg ([[Chairman]] and [[CEO]]) [[Sheryl Sandberg]] ([[Chief operating officer|COO]])";

// key_people obviously is a variable, and you only need it's vaue
$string = substr($string, strpos($string, " = "), strlen($string));

preg_match_all('/[^\)]+/', $string, $matches);

$normalized_array = array();
foreach($matches[0] as $index => $match) {
    $k = strrpos($match, "(") + 1;

    $person = substr($match, 0, $k - 1);
    $person = trim_linked_resources($person);

    $job_positions_raw = substr($match, $k, strlen($match));
    $job_positions = parse_linked_resources($job_positions_raw);


    $normalized_array[$index] = array(
        "person" => $person,
        "job_positions" => $job_positions,
        "job_positions_as_text" =>implode(" and ", $job_positions)
    );
}

print_r($normalized_array);

// The resources surrounded by [[]] we assume are linked resources.
function parse_linked_resources($string) {
    preg_match_all('/\[\[([^\]]+)\]\]/', $string, $matches);
    return $matches[1];
}

function trim_linked_resources($string) {
    $string = str_replace("[", "", $string);
    $string = str_replace("]", "", $string);    
    return $string;
}

输出如下:

Array
(
    [0] => Array
        (
            [person] =>  = Mark Zuckerberg 
            [job_positions] => Array
                (
                    [0] => Chairman
                    [1] => CEO
                )

            [job_positions_as_text] => Chairman and CEO
        )

    [1] => Array
        (
            [person] =>  Sheryl Sandberg 
            [job_positions] => Array
                (
                    [0] => Chief operating officer|COO
                )

            [job_positions_as_text] => Chief operating officer|COO
        )

)

可以进一步改进,但希望这对你的情况有用。