PHP regex find BBCode that is not surrounded by another BBCode
I'm using the following regex to find IMG bbcodes and their contents in forum posts:
~[img(?:=['"]?([^,]*?)(?:,[^]'"]+)?['"]?)?]([^[]+)?[/img]~i
This works so far, but i need to define exceptions. I must find all IMG bbcodes, which are NOT surrounded by a TT- or CODE bbcode. I'm not trying to parse BBCodes (because this is done by the forum software itself).
So i want the img bbcode from here (which is working, using the regex above):
Hello, this is an example: [img]xxx[/img] - Yay!
but not from there
[tt]this is a test [img]xxx[/img] yolo![/tt]
and not from here
[code=php]<?php
echo '[img=xxx][/img]';[/code]
Any idea, how to achieve this? I'm using PHP (just in case, that a regex-only-solution is not possible).
you could use this pattern against the second sub-pattern for your match
[((?:(?!img).)*?)](?:.*?)[/1]|[img.*](.*?)[/img]
http://regex101.com/r/tF1tX3/2
链接地址: http://www.djcxy.com/p/29880.html上一篇: 正则表达式匹配哈希标记,多行(PHP)