Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

comma separated numeric values

Last post 07-23-2008, 11:51 PM by ddrudik. 4 replies.
Sort Posts: Previous Next
  •  07-18-2008, 4:35 AM 44238

    comma separated numeric values

    Hi,

     In PHP I'm trying to filter the string looking for:
    [options=1,6,22,103]

    and then to extract numeric values into the array. As I just started my fun with regular expressions, the only way I can do it is like:
    preg_match('\/[options=(.*?)\]/', $content, $matches);

    But that will return numbers separated with commas, which I have to split them. There should be easier way to look for string starting by '[options=', read the values until finding ']' and return all results as a $matches array.

    Next step is to replace whole string (exemplary [options=1,6,22,103]) with something else (let's say 'success' text);
    Sth like:
    $content = preg_replace('/\[options=.*\]/', 'success', $content);


    Like you see, I need still more practice, but been fighting with that thing for quite a long time. Any help would be much appreciated.
    Big thanks in advance.
    Akashic

    Filed under: ,
  •  07-18-2008, 8:39 AM 44246 in reply to 44238

    Re: comma separated numeric values

    If you will just have 1 options= block in your source string:

    <?php
    $string='[options=1,6,22,103]';
    if(preg_match('/(?<=\[options=)[^]]*(?=\])/',$string,$match)){
      $result=explode(',',$match[0]);
      echo '<pre>'.print_r($result,true);
    }
    ?>

    If you will have multiple options= blocks then a few more lines of code will be required.


  •  07-20-2008, 7:40 PM 44384 in reply to 44238

    Re: comma separated numeric values

    You may be tempted to go one step further and think "why not grab the individual numbers while I'm at it" but this is not (in general) possible with many regex engines.

    The problem is this: the regex pattern:

    ((\d+),?)+

    WILL match the numbers separated by the comma or the end square bracket, but it will only return the last such number. The majority of regex engines will allow a match group to be used multiple times in creating an overall match, but they will only return the value of then LAST such match. Therefore, match group #2 in the above expression will firstly match the '1' and hold this text value. However, on the next iteration (after processing the ','), it will match the '6' and will over-write the "1" with "6".

    If you want to see this happening, then try this expression with the .NET regex engine in a tester such as Expresso. .NET introduces another level called 'captures' which sits below the 'match' and stores each individual value that is processed. However, as with most other regex's, the 'match group' value is still the last one found - it is only when you expand this down to the 'capture' level that you see all of the individual values.

    Therefore, the way you have gone about this is the only alternative when 'captures' are not available: treat the string of delimited numbers as a whole and the split them up separately as Doug has shown you.

    Susan 

  •  07-23-2008, 10:46 PM 44508 in reply to 44384

    Re: comma separated numeric values

    Thanks for the asnwers, I really appreciate them.

    As for PHP, I found in the def. of a function such thing:
    "If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on."

    As I might (or not?) understood it, the the futher searches will be stored in an array as well.
    But as I couldn't spend too much time playing with those functions, just used the following (for anyone that can benefit):

     

    //Get text and search for adequate string, than replace the result with function 'display'
    if(preg_match('/\[options=(.*)\]/', $content, $matches)) {
        $content = preg_replace('/\[options=.*\]/', display($matches[1]), $content);
    }  

    //what you want to display accordingly to the options given to input
    function display($ids) {
       $id = split(',', $ids);
    }

    So if you will have some page, that will have [options=9,12,17], the script will replace it with proper content (useful for dynamic, cofigurable data; I'm using it as a filter for plugins in WordPress)

  •  07-23-2008, 11:51 PM 44511 in reply to 44508

    Re: comma separated numeric values

    I think what you are looking for is preg_replace_callback which will allow for dynamic replacement of matches with function results, if you care to provide a specific example of what you want to do based on the match example possibly I could provide some useful code.
View as RSS news feed in XML