Preg_Match Problem - Please Help!

Discussion in 'PHP' started by projectWORD, Apr 1, 2010.

  1. #1
    Hi,

    I have this XML code which I am trying to get all category A's from:-

    <categoryA>Data1.1</categoryA><categoryB>Data 2.1</categoryB>
    <categoryA>Data1.2</categoryA><categoryB>Data 2.2</categoryB>
    <categoryA>Data1.3</categoryA><categoryB>Data 2.3</categoryB>
    <categoryA>Data1.4</categoryA><categoryB>Data 2.4</categoryB>
    <categoryA>Data1.5</categoryA><categoryB>Data 2.5</categoryB>
    <categoryA>Data1.6</categoryA><categoryB>Data 2.6</categoryB>

    requirements are that I use reg exp and I need to do other things.

    I have this code:-

    preg_match('/<categoryA>(.*)</categoryA><categoryB>/',$input,$output);

    Where it matches but it gives me:-

    Data1.1</categoryA><categoryB>Data 2.1</categoryB>
    <categoryA>Data1.2</categoryA><categoryB>Data 2.2</categoryB>
    <categoryA>Data1.3</categoryA><categoryB>Data 2.3</categoryB>
    <categoryA>Data1.4</categoryA><categoryB>Data 2.4</categoryB>
    <categoryA>Data1.5</categoryA><categoryB>Data 2.5</categoryB>
    <categoryA>Data1.6</categoryA>

    So it gives me the match that is my category but then goes all the way past my categoryB tag to the end categoryB tag.

    How do I tell it to ensure it stops at the first categoryB tag or something like this

    Thanks in advance
     
    projectWORD, Apr 1, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Here is the right code:

    
    preg_match_all('|<categoryA>(.*)</categoryA>|',$input,$output);
    print_r($output);
    
    PHP:
     
    stephan2307, Apr 1, 2010 IP
  3. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Hi, this doesnt work sorry. It carrys on after /categoryA
     
    projectWORD, Apr 1, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    not when I tested it.

    Have you checked the print_r($output) properly?
     
    stephan2307, Apr 1, 2010 IP
  5. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #5
    With this code:
    <?php
    $input = "
    <categoryA>Data1.1</categoryA><categoryB>Data 2.1</categoryB>
    <categoryA>Data1.2</categoryA><categoryB>Data 2.2</categoryB>
    <categoryA>Data1.3</categoryA><categoryB>Data 2.3</categoryB>
    <categoryA>Data1.4</categoryA><categoryB>Data 2.4</categoryB>
    <categoryA>Data1.5</categoryA><categoryB>Data 2.5</categoryB>
    <categoryA>Data1.6</categoryA><categoryB>Data 2.6</categoryB>
    ";
    
    preg_match('/<categoryA>(.*?)<\/categoryA>/m',$input,$output);
    // If you don't like the \/ sequence you can use :
    // preg_match('#<categoryA>(.*?)</categoryA>#m',$input,$output); 
    
    var_dump($output);
    
    ?>
    PHP:
    I get this:
    array(2) {
      [0]=>
      string(30) "<categoryA>Data1.1</categoryA>"
      [1]=>
      string(7) "Data1.1"
    }
    
    Code (markup):
    You should not rely on the browser display as the output contains tags... You should view the page html source code...
     
    Last edited: Apr 1, 2010
    nabil_kadimi, Apr 1, 2010 IP