0

im having an issue with preg_match_all. I have this string:

     $product_req = "ACTIVE-6,CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-9";

I need to get the numbers preceded by "ACTIVE-" but not by "CATEGORY-ACTIVE-", so in this case the result should be 6,9. I used the statement below:

      preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act);

However this will return all the numbers because all of them are in fact preceded by "ACTIVE-" but thats not what i meant because i need to leave out those preceded by "CATEGORY-ACTIVE-". How can i configure preg_match_all to do it? Or maybe there is some other function that can do the job?

EDIT:

I tried this:

      preg_match_all("/CATEGORY-ACTIVE-(\d+)/", $product_req, $this_cat_act);
      preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act);
      $act_cat = str_replace($this_cat_act[1],"",$this_act[1]);

it kinda works, but i guess there is a better and cleaner way to do it. Besides the output is kinda weird too.

Thank you.

Cain Nuke
  • 1,865
  • 4
  • 29
  • 52
  • Couldn't you use the quantifier? `{0}` I haven't tried, so that's why not posting as an answer. –  Apr 11 '14 at 03:07
  • `preg_match_all("/{CATEGORY-){0}ACTIVE-(\d+)/", $product_req, $this_act);` –  Apr 11 '14 at 03:10
  • I didnt know about that. However i tried it now but i get this error: unmatched parentheses at offset – Cain Nuke Apr 11 '14 at 03:15
  • Oh, hell... tired man typing `preg_match_all("(CATEGORY-){0}/ACTIVE-(\d+)/", $product_req, $this_act);` –  Apr 11 '14 at 03:16
  • Sorry, now it says Unknown modifier '{' – Cain Nuke Apr 11 '14 at 03:19
  • grr... the dash? (I did say I hadn't tested, lol.) `preg_match_all("/(CATEGORY\-){0}ACTIVE-(\d+)/", $product_req, $this_act);` (needed to move the /, too, sigh.) –  Apr 11 '14 at 03:20
  • No there is no error but i got this output: Array ( [0] => [1] => [2] => ) – Cain Nuke Apr 11 '14 at 03:21
  • I give up for tonight, then. I did find http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word which seems quite promising. –  Apr 11 '14 at 03:24
  • Okay, thanks. Though im still lost. – Cain Nuke Apr 11 '14 at 03:31

0 Answers0