I'm trying to create a form builder that my users can use with a homegrown markup inspired by BBCode converting their questions into actual HTML I've got a regex "fiddle" up at https://regex101.com/r/5uf7fK/1 I have two examples Name: [TEXT|enter your name here] Type of tenancy: [RADIO|This is a periodic tenancy,This tenancy ends on [TEXT|enter date]] Code (markup): and the regex I'm using is preg_match_all("/\[([^\]]*)\]/", $block, $matches); PHP: So here's what I'm getting Ideally, I'd like to work with the full match result and the first result is good. In the second result, my expected output is: [RADIO|This is a periodic tenancy,This tenancy ends on [TEXT|enter date]] Code (markup): but the regular expression trims off the second ] Anyone know how I can edit that so that it returns both ] ?
It's not a single RegEx that provides your solution. You could have multiple nested [] tags anywhere within a [] tag.... This *can* get quite complex to parse.... Your best option is to use a BBCode parsing library and edit accordingly to support your makeshift bbcode.... My suggestion is to use PHP's "BBCode" extension or use a makeshift class that exists and is well tested then edit to your spec. IF your "bbcode" is nothing more than replaceable tags or simple blocks of tags you could end up doing something as simple as a preg_replace... I have an example but this forum won't accept the code Edit: Simple example : https://tehplayground.com/ipevj5c4WOhWYRWl
Thanks for that. I've ended up allowing checkboxes and radios to have nested fields and used < for them. The name and id structure for the inputs is reasonably complicated so a lot goes on when building them including allowing for replications of groups of inputs. Splitting the radio options into individual bbcode items might have been good but linking them back together, especially when replicated, might have been messy.
Why are you not using a BB code like this one: [text label="Enter your name" required="yes"] [radio label="select option" options="abc, xyz, fgh" required="no"] This will be much easier to parse with both regex and PHP.
@JEET - that would probably be better. I'll look at what's involved in converting. I'd still need to be able to nest.
To have something nested, you can try to use a bb code like this one: [radio label="something" option="ABC" option="XYZ" option="fgh" required="no"] Now your first preg_match will return everything inside brackets, and if its a type "radio", then do another preg_match to find all "options" inside this. I think it might look like this: $string= ' this is something [text label="something"] [radio label="radio button" option="ABC" option="FFF" required="no"] and some more'; preg_match_all( '/\[(.*?)\]/is', $string, $match ); foreach( $match[1] as $k ){ if( substr( trim($k), 0, 5 ) == 'radio' ){ preg_match_all( '/(option\=\")(.*?)(\")/is', $k, $options ); //list of options is in $options[2] }//if ends }//foreach ends