Search code examples
phppreg-matchmediawiki

grab category value from mediawiki markup in php


I am working on retriving category values from a wiki markup text in loop, could not grab category values from the markup using regex match in php

The Markup Text Contains the category values as

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

Here's what I have tried so far

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

This is the output

 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
)

But I'm trying to get output array with only category values after colon , i.e.

 Array
(
    [0] => Google
    [1] => Tricks
    [2] => Google Searcg
)

And so on.

What changes should i make to my regex to get only category values filled up in the $mathces array Or should i use oter php function instead of preg_match ?

Kindly note that, the $input_wiki_markup also containes other text around the [[Categpry:xyz]] tags


Solution

  • all you need was an all

    $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);
               }
    

    OUTPUT:

    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
        )
    

    )