Log in

View Full Version : regex replacement, with any contents



djr33
07-21-2007, 02:54 PM
I'm trying to understand regex more, so this is just an exploratory question.

Let's assume (without going into any security issues, here), that I wanted to parse BB code as html.
The format would be ... which would become <tag>...</tag>

My question is how to keep the middle of that, the name of the tag, and replace it in the output.

In other words, replace '[' followed by any letters/numbers, no spaces (assume no parameters for now) then ']' with '<', the middle bit (letters/numbers), then '>'.

The main question is how you keep the unknown match portion (any letters/numbers) and put that same portion into the output.

I hope the question is clear enough.

Or a simpler question would be "find any pattern of number-letter-number-letter in the page, then duplicate that three times with hyphens between".
On the page, for example, "1a1a" would become "1a1a-1a1a-1a1a"

Twey
07-21-2007, 03:13 PM
$html = preg_replace('/\\[(\\/?[a-zA-Z0-9]+?)\\]/', '<$1>', $bbcode);
$repeated = preg_replace('/(\\d\\w\\d\\w)/', '$1-$1-$1', $original);

djr33
07-21-2007, 03:21 PM
That's easy, then. How is $1 defined, though? For the first example, you also have [ and ] as part of the search, which you would not (or might in some cases) want included in the replace. How is this determined?

EDIT: Ok, found it as PHP.net:
replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern.
Thanks. Good to know.

Twey
07-21-2007, 04:05 PM
Note that that's in single quotes: $1 isn't interpreted as a variable.