/    Sign up×
Community /Pin to ProfileBookmark

Problem with preg_match_all

Hi,

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

[code=php] $product_req = “ACTIVE-6,CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-9”;[/code]

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 first used the statement below:

[code=php] preg_match_all(“/ACTIVE-(d+)/”, $product_req, $this_act);[/code]

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-“. Then i tried this:

[code=php] 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]);[/code]

it kinda works, but i guess there is a better and cleaner way to do it. Besides the output is kinda weird too. How can i configure preg_match_all to do it? Or maybe there is some other function that can do the job?

Thank you.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 11.2014 — I think (but have not tested), using look-behind assertions (both negative and positive):
[code=php]
<?php
$regex = '/(?<!-)(<?=ACTIVE-)d+/';
preg_match_all($regex, $product_req, $act_cat);
[/code]
×

Success!

Help @supercain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...