GivenPie GivenPie - 3 years ago 97
PHP Question

How to utilize method with RegEx in order to print into a dynamic table on with PHP

I have a list of courses and the hours they require for students to take them. The courses are as follows:

CON8101 Residential Building/Estimating 16 hrs/w
CON8411 Construction Materials I 4 hrs/w
CON8430 Computers and You 4 hrs/w
MAT8050 Geometry and Trigonometry 4 hrs/w


I have used this RegEx to extract the name of course and the hours each course takes each week. There are more than 4 courses, the 4 are examples above. There can be as many as 50 courses.

$courseHoursRegEx = "/\s[0-9]{1,2}\shrs/w/";
$courseNameRegEx = "/[a-zA-Z]{3}[0-9]{4}[A-Z]{0,1}\s?/[a-zA-Z]{3,40}/";


And applied the following function (not sure if 100% right) to extract the RegEx'd strings. Using $courseLine is the variable I saved the string of each line from a text document that early I have fopened. It keeps track of the total hours that has been extracted from the string.

$courses is an array of check boxes that the user enters in the html section

$totalHours += GetCourseHours($courseLine);
function GetCourseHours($couseLine)
{
if(!preg_match($courseHoursRegEx, $courseLine))
{
return $courseLine;
}
}
function GetCourseName($courseLine)
{
if(!preg_match($courseNameRegEx, $courseLine))
{
return $courseLine;
}
}


I used a foreach loop to output all the selected courses to be sorted out in a table.

foreach($courses as $course)
{
$theCourse = GetCourseName($course);
$theHours = GetCourseHours($course)
}


Edit: output code

for($i = 1; $i <= $courses; ++$i)
{
printf("<tr><td>\$%.2f</td><td>\$%.2f</td></tr>", $theCourse, $theHours);

}


I am not sure how to output what I have into a dynamic table organized by the course name, and hours for each course. I cannot get my page to run, I cannot find any syntax errors, I was afraid it was my logic.

Answer Source

First of all, (after fixing a few minor things within the regexes) you can do all of that in one preg_ call. Here is how:

preg_match_all("~([a-zA-Z]{3}\d{4}[A-Z]{0,1}\s.+)\s(\d{1,2})\shrs/w~", $str, $matches);

$str can either be a multiline string with all rows at once. Or you can pass in a single line at a time. If you pass in all lines at once, $matches will afterward look like this:

Array
(
    [0] => Array
        (
            [0] => CON8101 Residential Building/Estimating 16 hrs/w
            [1] => CON8411 Construction Materials I 4 hrs/w
            [2] => CON8430 Computers and You 4 hrs/w
            [3] => MAT8050 Geometry and Trigonometry 4 hrs/w
        )

    [1] => Array
        (
            [0] => CON8101 Residential Building/Estimating
            [1] => CON8411 Construction Materials I
            [2] => CON8430 Computers and You
            [3] => MAT8050 Geometry and Trigonometry
        )

    [2] => Array
        (
            [0] => 16
            [1] => 4
            [2] => 4
            [3] => 4
        )

)

Now you can simply iterate over all names in $matches[1] and sum up the hours in $matches[2]. Notice that those two inner arrays correspond to what's inside of the round brackets I used in the regex. These are so called subpatterns, and they capture additional (sub-)matches. Also $matches[0] will always contain the full match of the whole pattern, but you don't need that in this case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download