-1

I am trying to locate all matching strings in my content with preg_match_all() but it is returning some random results.

Here's my content:

This is my content but it is completely dynamic...
[[hello_world]]
[[hello-world]]
[[hello world]]

I am wanting to only return this: [[hello_world]]

preg_match_all('/[[[a-z0-9_]+?]]/', $content, $matches);

but it is returning this:

0 => "[[hello_world]]"
1 => "world]]"
2 => "world]]"

I am wanting to only return this: [[hello-world]]

preg_match_all('/[[[a-z0-9-]+?]]/', $content, $matches);

but it is returning this:

0 => "world]]"
1 => "[[hello-world]]"
2 => "world]]"

I am wanting to only return this: [[hello world]]

preg_match_all('/[[[a-z0-9 ]+?]]/', $content, $matches);

but it is returning this:

0 => "world]]"
1 => "world]]"
2 => "[[hello world]]"

Thanks for the help!

LargeTuna
  • 2,694
  • 7
  • 46
  • 92

1 Answers1

0

Solution below :

EDIT : I modified the regex because it is not limited to the requested characters.

preg_match_all('/\[\[(.[a-z0-9-_ ]*)\]\]/s', $input_lines, $output_array);
Maxime D
  • 131
  • 1
  • 6