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
Here is the right code: preg_match_all('|<categoryA>(.*)</categoryA>|',$input,$output); print_r($output); PHP:
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...