Regex-get integer between tags

Discussion in 'PHP' started by php_techy, Jan 11, 2010.

  1. #1
    Hi,
    Please excuse if similar issue has been posted earlier.
    My data is like

    I am going [ABCDEF]123[/ABCDEF] to movie.
    I want a regex to get the value between [ABCDEF] and [/ABCDEF] only if its integer.
    eg if its [ABCDEF]junk characters[/ABCDEF] then I dont want...but if its [ABCDEF]123[/ABCDEF]
    then I require 123

    Regards
     
    php_techy, Jan 11, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Here you go:

    The regex: #^\[ABCDEF\]([0-9]*)\[/ABCDEF\]$#

    <?php
    
    $data = <<<DATA
    [ABCDEF]123[/ABCDEF]
    DATA;
    
    if(preg_match("#^\[ABCDEF\]([0-9]*)\[/ABCDEF\]$#", $data, $matches)) {
    
    print($matches[1]);
    
    } else {
    
    print("No matches found");
    
    }
    
    ?>
    PHP:
    [ABCDEF]123[/ABCDEF] = is valid

    [ABCDEF]junk characters[/ABCDEF] = not valid
     
    danx10, Jan 11, 2010 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you have a multitude of these patterns in your data, remove the ^ and $ from the pattern above and use the preg_match_all() function
     
    JAY6390, Jan 11, 2010 IP