Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2010, 05:58 PM   PM User | #1
1337c0d3r7001
New to the CF scene

 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
1337c0d3r7001 is an unknown quantity at this point
Lightbulb 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 ) )

Last edited by 1337c0d3r7001; 08-14-2010 at 06:04 PM..
1337c0d3r7001 is offline   Reply With Quote
Old 08-14-2010, 06:30 PM   PM User | #2
disastro
Regular Coder

 
Join Date: Jul 2010
Posts: 185
Thanks: 3
Thanked 42 Times in 42 Posts
disastro is on a distinguished road
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 is offline   Reply With Quote
Old 08-14-2010, 06:34 PM   PM User | #3
disastro
Regular Coder

 
Join Date: Jul 2010
Posts: 185
Thanks: 3
Thanked 42 Times in 42 Posts
disastro is on a distinguished road
Sorry, didn't notice you were using it for $temp3.

I'll take a closer look this time
disastro is offline   Reply With Quote
Old 08-14-2010, 06:35 PM   PM User | #4
1337c0d3r7001
New to the CF scene

 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
1337c0d3r7001 is an unknown quantity at this point
thank you very much for your replies
1337c0d3r7001 is offline   Reply With Quote
Old 08-14-2010, 07:33 PM   PM User | #5
1337c0d3r7001
New to the CF scene

 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
1337c0d3r7001 is an unknown quantity at this point
I got is resolved at stack overflow. it is actually a problem with the double space in the first subject's tags.
1337c0d3r7001 is offline   Reply With Quote
Old 08-14-2010, 07:54 PM   PM User | #6
disastro
Regular Coder

 
Join Date: Jul 2010
Posts: 185
Thanks: 3
Thanked 42 Times in 42 Posts
disastro is on a distinguished road
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); 
disastro is offline   Reply With Quote
Users who have thanked disastro for this post:
1337c0d3r7001 (08-14-2010)
Old 08-14-2010, 07:59 PM   PM User | #7
1337c0d3r7001
New to the CF scene

 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
1337c0d3r7001 is an unknown quantity at this point
That really did work. Thanks disastro for your time.
1337c0d3r7001 is offline   Reply With Quote
Reply

Bookmarks

Tags
preg_match_all, regex

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.