www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problem with preg_match_all

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    Problem with preg_match_all

    Hi,

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

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

    PHP Code:
          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-". Then i tried this:

    PHP Code:
          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. How can i configure preg_match_all to do it? Or maybe there is some other function that can do the job?

    Thank you.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,407
    I think (but have not tested), using look-behind assertions (both negative and positive):
    PHP Code:
    <?php
    $regex 
    '/(?<!-)(<?=ACTIVE-)\d+/';
    preg_match_all($regex$product_req$act_cat);
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles