简体   繁体   中英

css / jQuery attribute selector in php

In the question I'm essentially looking for some help with a regular expression.

Here is the pattern I'm currently using:

 preg_match_all("~\[([^\]]+)=([^\[]+)\]~", $search, $matches, PREG_SET_ORDER);

That works fine for :

'[name=test][type=select]'

Result:

MATCH 1
1.  [1-5]   `name`
2.  [6-10]  `test`
MATCH 2
1.  [12-16] `type`
2.  [17-23] `select`

demo

The problem comes with

'[for=event[schedule]]'

Obviously whats happening is the extra [ 's and ] 's prevent the regex from matching the string.

I'm looking for suggestions on how to fix the regex pattern to get this output:

MATCH 1
1.  [1-3]   `for`
2.  [5-19]  `event[schedule]`

If you want to optionally match the [...] part:

preg_match_all('~\[([^\]]+)=([^\]\[]+(?:\[[^\]]+\])?)\]~', $search, $matches, PREG_SET_ORDER);

To require it:

preg_match_all('~\[([^\]]+)=([^\]\[]+\[[^\]]+\])\]~', $search, $matches, PREG_SET_ORDER);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM