views:

49

answers:

2

Hi,

I'm a PHP newbie. I'm trying to use preg_match_all function in the below program to find all subjects with their marks, but I'm getting only one match. I have been struggling with this for 5 hours. Can someone help me in figuring out whats wrong? Thanks in advance.

<?php
$semArray="<B>STUDENTS NAME (7ab05cs001) </B><br><br><br><br><hr><table><tr><td><b>Semester:</b></td><td><b>2</b></td><td></td><td> &nbsp;&nbsp;&nbsp;&nbsp;<b> Result:&nbsp;&nbsp;FIRST CLASS </b></td></tr></table><hr><table><tr><td width=250>Subject</td><td width=60 align=center>External </td><td width=60 align=center>Internal</td><td align=center width=60>Total</td><td align=center width=60>Result</td></tr><br><tr><td width=250><i>Engineering Maths - II (06MAT21)</i></td><td width=60 align=center>51</td><td width=60 align=center>16</td><td width=60 align=center>67</td><td  width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Engineering Chemistry (06CHE22)</i></td><td width=60 align=center>40</td><td width=60 align=center>17</td><td width=60 align=center>57</td><td width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Computer Concepts and C Programming (06CCP23)</i></td><td width=60 align=center>70</td><td width=60 align=center>23</td><td width=60 align=center>93</td><td width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Computer Aided Engineering Drawing (06CED24)</i></td><td width=60 align=center>50</td><td width=60 align=center>16</td><td width=60 align=center>66</td><td width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Basic Electronics (06ELN25)</i></td><td width=60 align=center>42</td><td width=60 align=center>17</td><td width=60 align=center>59</td><td width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Computer Programming Lab (06CPL26)</i></td><td width=60 align=center>46</td><td width=60 align=center>24</td><td width=60 align=center>70</td><td width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Engg. Chemistry Lab (06CHEL27)</i></td><td width=60 align=center>41</td><td width=60 align=center>19</td><td width=60 align=center>60</td><td width=60 align=center><b>P</b></td></tr><tr><td width=250><i>Environmental Studies (06CIV28)</i></td><td width=60 align=center>48</td><td width=60 align=center>25</td><td width=60 align=center>73</td><td width=60 align=center><b>P</b></td></tr></table><br><br><table><tr><td></td><td></td><td>Total Marks:</td><td> 545 &nbsp;&nbsp;&nbsp; </td></tr></table>";
function get_result_for_this_sem($semArray)
{

preg_match("/Semester:<\/b><\/td><td><b>(.)<\/b>/",$semArray,$temp1);
$sem_no=$temp1[1];
preg_match("/Result:&nbsp;&nbsp;(.+)<\/b><\/td><\/tr><\/table><hr><table>/U",$semArray,$temp2);
$sem_final_result=$temp2[1];
preg_match_all("/<i>((.+?)\((.+?)\))<\/i><\/td><td width=60 align=center>([0-9]{1,3})<\/td><td width=60 align=center>([0-9]{1,2})<\/td><td width=60 align=center>([0-9]{1,3})<\/td><td  width=60 align=center><b>(.)<\/b><\/td><\/tr>/",$semArray,$temp3,PREG_SET_ORDER);

print_r($temp3);

}
get_result_for_this_sem($semArray);
?>

Here is the output that I get:

Array ( [0] => Array ( [0] => Engineering Maths - II (06MAT21)511667P  [1] => Engineering Maths - II (06MAT21) [2] => Engineering Maths - II [3] => 06MAT21 [4] => 51 [5] => 16 [6] => 67 [7] => P ) )
A: 

Check this one: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

Sam Dark
This is not an answer to the question.
Gumbo
thank you Sam Dark for your reply and Gumbo for your comments
+2  A: 

You've copied and pasted the literal HTML into your regex. There's a double-space in the first subject's section that doesn't exist in the following sections, and you're matching that double space literally.

<td  width=60 align=center><b>P</b></td>
JimG
Thank u for ur reply.sorry but i did not understand it properly. can u suggest some solution please?
A better solution, use an HTML parser: http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php
George Marian
Remove the double space from both the data and the regex. That will solve the immediate problem (though not the deeper problem, which is that regex is really the wrong tool for the job).
JimG
thanks guys.... u rock