Regex expression help

Hi,

I was hoping one of you regex gurus could help me with my expression here…

I’m trying to match this…

[widget:somewidget.php?var1=val1&var2=val2]

Here is the regex

/\\[widget:([a-zA-Z0-9._-]*?)\\?([a-zA-Z0-9&_=-]*?)\\]/e

it matches perfectly as is if there is only 1 variable in the query string but I’d like to be able to handle as many as I give it.

Any help appreciated!

That did the trick! Thank you sir!

You’re most welcome. :smiley:

Welcome to SitePoint! Don’t be a stranger.

Give this a go. :wink:


<?php
preg_match(
  '~\\[widget:[^?]+\\?([^\\]]+)\\]~',
  '[widget:somewidget.php?var1=val1&var2=val2]',
  $matches
);

parse_str(
  $matches[1],
  $elements
);

print_r(
  $elements
);

/*
  Array
  (
      [var1] => val1
      [var2] => val2
  )
*/
?>