regex operator OR - preg_match_all

87 views Asked by At

I have string {tender id="2,3"} or {tender catid="2" id="2,3"} or {tender catid="2"}

I use

$regex = "/{tender ((.*?id\s*=\s*['\"](.*?)['\"]) (.*?id\s*=\s*['\"](.*?)['\"]))}/";

but work only if i have in string both id and catid....How can i write regex to work if only one parameter is in string?

1

There are 1 answers

0
Laurel On BEST ANSWER

Try: {tender (?:cat)?id="(\d+,?)+"}

You may need to specify additional details if it doesn't work for you. As you requested, it will only match when either catid or just id is in the data structure (case 1 and 3 for your examples).

Explanation

  • {tender the start of the data.
  • (?:cat)? catch cat if it can
  • id=" gets the start of an id
  • (\d+,?)+ gets digits that may or may not be followed by a comma
  • "} End of the data.