我正在循环中从wiki标记文本检索类别值,无法使用php中的regex match从标记中获取类别值
标记文本包含下列类别值:

$input_wiki_markup = "
[[Category:Google]]
[[Category:Tricks]]
[[Category:Google Search]]
[[Category:Filters]]
[[Category:Search]]
[[Category:Tips]]";

这是我到目前为止试过的
$matches = array();
           if(preg_match("/\[\[(Category):(.+)*\]\]/i", $input_wiki_markup, $matches)){
               print_r($matches);
           }

这是输出
 Array
(
    [0] => [[Category:Google]][[Category:Tricks]][[Category:Google Search]][[Category:Filters]][[Category:Search]][[Category:Tips]]
    [1] => Category
    [2] => Google]][[Category:Tricks]][[Category:Google Search]][[Category:Filters]][[Category:Search]][[Category:Tips
)

但我试图得到只包含冒号后的类别值的输出数组,即。
 Array
(
    [0] => Google
    [1] => Tricks
    [2] => Google Searcg
)

等等。
为了在$mathces数组中只填充类别值,我应该对regex做什么更改?
还是应该使用oter php函数而不是preg_match?
请注意,$input_wiki_markup还包含[[Categpry:xyz]]标记周围的其他文本

最佳答案

你只需要一个

$input_wiki_markup="
[[Category:Google]]
[[Category:Tricks]]
[[Category:Google Search]]
[[Category:Filters]]
[[Category:Search]]
[[Category:Tips]]
";

$matches = array();
           if(preg_match_all("/\[\[(Category):(.+)*\]\]/i", $input_wiki_markup, $matches)){
               print_r($matches);
           }

输出:
Array
(
[0] => Array
    (
        [0] => [[Category:Google]]
        [1] => [[Category:Tricks]]
        [2] => [[Category:Google Search]]
        [3] => [[Category:Filters]]
        [4] => [[Category:Search]]
        [5] => [[Category:Tips]]
    )

[1] => Array
    (
        [0] => Category
        [1] => Category
        [2] => Category
        [3] => Category
        [4] => Category
        [5] => Category
    )

[2] => Array
    (
        [0] => Google
        [1] => Tricks
        [2] => Google Search
        [3] => Filters
        [4] => Search
        [5] => Tips
    )

关于php - 从PHP中的MediaWiki标记中获取类别值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34733270/

10-11 18:05