I can’t work out how to get my Regex to select multiple lines, when I’m using
[b]Test[/b]
my BB code works fine but when I try
[b]
Test
[/b]
I have read that I should use the modifiers /s /m in my regex but I don’t know how to, I have tried: preg_match_all(‘\[(' . $tags . ')=?(.*?)\](.+?)\[/\1\]/m/s‘ but it doesnt work.
Any Suggestions?
Working example:
$tags = 'b|i|size|color|center|quote|url|img';
while (preg_match_all('`\[(' . $tags . ')=?(.*?)\](.+?)\[/\1\]`', $string, $matches))
foreach ($matches[0] as $key => $match) {
list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
Your syntax for adding modifiers is not right. Firstly, you do not need any slashes to apply them. They just belong after the delimiter (which in your case is not
/but`). And also you do not need to delimit every single modifier again, just stick them together:Which delimiter you use does not matter at all. Check out PHP documentation on delimiters. You can use
Just be careful that the delimiter has to be escaped. So there is no difference between
`and/at all. Just if you use/instead, then you need to escape/within the regex (otherwise PHP will assume that this is the end of the regex). Like this:Therefore, the choice of delimiter is mostly a matter of convenience – use one that does not occur within the regex if possible, so you do not have to escape it.
/is simply the most common choice, which is why you have probably found the/mnotation for adding modifiers. As the PHP documentation states are common choices are#and~(I have also seen!quite often).