Regular expression issue

Discussion in 'PHP' started by silviuks, Dec 9, 2010.

  1. #1
    Hello,

    I'm kind of newbie working with regular expression and I came to a dead end.

    So, this is the code:

    #HEADER#
    Agent: 1544662
    Rows: 2
    Date: 09-12-2010
    
    #DEFINITION#
    some other informations ....
    
    Code (markup):
    I want to get the content between #HEADER# AND #DEFINITION#
    Can you give me some clues how to solve this problem?

    Thanks,
    Silviu
     
    silviuks, Dec 9, 2010 IP
  2. mikecampbell

    mikecampbell Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If it's that simple, you don't need a regular expression. You can just use strpos to find #HEADER# and #DEFINITION# and then substr to get the text between them.
     
    mikecampbell, Dec 9, 2010 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    I would do a regular expression, string replace then PHP's built in parser:

    If you are the one creating the original file, then do it as a INI file:

    [Section]
    option = value

    
    $string = '#HEADER#
    Agent: 1544662
    Rows: 2
    Date: 09-12-2010
    
    #DEFINITION#
    Variable: value';
    
    $string = preg_replace('/#(.*)#/','[$1]',$string);
    $string = str_replace(':','=',$string);
    
    $test = parse_ini_string($string,true);
    
    print_r($test);
    
    PHP:
     
    ThePHPMaster, Dec 9, 2010 IP
  4. silviuks

    silviuks Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks guys. I solve the problem using strpos as mike said.
     
    silviuks, Dec 11, 2010 IP
  5. Eager2Seo

    Eager2Seo Member

    Messages:
    72
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    Think of regex as a last resort, not for something that simple. Regex is very slow.
    Also, when using regex chop the string down to the section that needs searching. Example, if you need to search within a table on an html page reduce to string to that chunk. If the script gets a lot of hits it could make a difference.
     
    Eager2Seo, Dec 11, 2010 IP