CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Urgent Help Needed on PHP Regex (http://www.codingforums.com/showthread.php?t=202387)

1337c0d3r7001 08-14-2010 05:58 PM

Urgent Help Needed on PHP Regex
 
hi i am php newbie.i am trying to use preg_match_all function in the below program to find all subjects with their marks but i am getting only ONE match.i have been struggling with this for 5 hrs. can someone help me in figuring out whats wrong? thanks in advance

PHP Code:

<?php
$semArray
="<B>STUDENTS NAME (6aa05cs001) </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:
Code:

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 ) )

disastro 08-14-2010 06:30 PM

Use preg_match_all()

preg_match() stops after the first match.

Documentation:
http://www.php.net/manual/en/functio...-match-all.php
http://www.php.net/manual/en/function.preg-match.php

disastro 08-14-2010 06:34 PM

Sorry, didn't notice you were using it for $temp3. :o

I'll take a closer look this time :D

1337c0d3r7001 08-14-2010 06:35 PM

thank you very much for your replies

1337c0d3r7001 08-14-2010 07:33 PM

I got is resolved at stack overflow. it is actually a problem with the double space in the first subject's tags.

disastro 08-14-2010 07:54 PM

Good to hear.

I was just playing around with this:
PHP Code:

preg_match_all('@<tr><td width=250><i>(.+?)</i></td>(<td width=60 align=center>(.+?)</td>)+</tr>@',$semArray$strn,PREG_SET_ORDER); 


1337c0d3r7001 08-14-2010 07:59 PM

That really did work. Thanks disastro for your time.


All times are GMT +1. The time now is 01:37 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.