Search code examples
phppreg-match

php replace multiple placeholders in text returns 1 element instead 2


I have a string and i want to return 2 placeholder in an array, how can i achieve that?

$text = 'is simply dummy text of the printing and [[file::aaa]] typesetting industry. Lorem Ipsum has been the [[file::bbb]] standard dummy text ever since the 1500s.';

if (preg_match("/\[\[.*\]\]/", $text, $matches)) {
    print_r($matches);
}

Solution

  • You could trying something like this :

    if (preg_match_all("/\[\[(.*?)\]\]/", $text, $matches)) {
        print_r($matches);
    }
    

    Outputs:

    Array
    (
        [0] => Array
            (
                [0] => [[file::aaa]]
                [1] => [[file::bbb]]
            )
    
        [1] => Array
            (
                [0] => file::aaa
                [1] => file::bbb
            )
    
    )