Sjwdavies
Sjwdavies

Reputation: 4173

PHP preg_match_all: Extract comma separated list

I have, for example, the following string:

{WIDGET_TEST('abc','456')}

I'd like to be able to use preg_match_all to return an array of the comma separated arguments.

Can anybody help me out with the regular expression I need?

I've tried and the following query returns (a,b):

preg_match_all('/\([a-zA-Z0-9\',]+\)/', '{WIDGET_TEST(a,b)}', $arguments);

But I'm left fighting to get this result as an array, and the regex breaks when I introduce the apostrophes?

EDIT Using the following:

preg_match_all('/(\'[a-zA-Z0-9,]+\')/', '{WIDGET_TEST('variable1','b')}', $arguments);

I get:

array
  0 => 
    array
      0 => string ''variable1'' (length=11)
      1 => string ''b'' (length=3)
  1 => 
    array
      0 => string ''variable1'' (length=11)
      1 => string ''b'' (length=3)

So believe I am part way there...

Upvotes: 0

Views: 989

Answers (1)

mr_mark88
mr_mark88

Reputation: 1059

I think if you try the following regex it should work for what you need:

preg_match_all('/([a-zA-Z0-9_]+)/', '{WIDGET_TEST(\'variable1\', \'b\')}', $classname);

It should return, from the input string, the strings that are constructed from ‘abcdEDFGHJ123’. To put it bluntly, it will return a new result when the string breaks from this makup.

Upvotes: 1

Related Questions