RealFantasy
RealFantasy

Reputation: 35

php preg_replace pattern

I need a hand on my preg_replace pattern:

I want to replace the texts between brackets [] but also inside brackets and not only until the first one [.[.].....] and the same pattern but to replace only numbers inside those brackets.

any idea?

Upvotes: 1

Views: 626

Answers (1)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99879

Try this regex:

$re = '#\[(?:.*?(?0))*.*?\]#'

This will match a [...] pair, which may itself contain one or more [...] pairs, with any characters between them. This is done by using recursion in the pattern (the (?0) calls the pattern again).

preg_match($re, '[.[.]....]', $m);
print_r($m);

// Output:
// Array
// (
//    [0] => [.[.]....]
// )

Upvotes: 4

Related Questions