Jump to content

preg_match_all


branmh

Recommended Posts

I'm having issues with preg_match_all, my information I used changed and doesn't match the previous code it was using. The table below is only showing 2 row's but it does contain about 30 row's. I have also attached the old code for reference. I just don't understand why or how to put a new line in between the <td></td>'s. 

  						if (preg_match_all('/<td>(\d+)<\/td><td.+?>([\d\:]+)<\/td><td>([\w\d\s]+)<\/td><td>([\d\.]+)<\/td><td.+?>([^,\n]+)<\/td>' .
  									'<td>([^,\n]+)<\/td><td>(-?\d+)<\/td><td>(-?\d+)<\/td>' . '\s+?<td>(?-?\d+)|)<\/td><td>(?-?\d+)|)<\/td>' . '<td>([\d]+\%)<\/td><td>(NA|[\d\.]+)<\/td><td>(NA|[\d\.]+)<\/td><td>([\d\.]+)<\/td>' .
  									'<td>(NA|[\d\.]+)<\/td><td>(|\d+\.\d+)<\/td><td>(|\d+\.\d+)<\/td><td>(|\d+\.\d+)<\//s', $raw, $m))
  						{
 
        <div id="obs-site">
            <table class="data-table" cellpadding=0 cellspacing=0 border=0>
                                        <tr>
                        <td>Apr 27 18:53</td>
                        <td>E 10</td>
                        <td>10</td>
                        <td>Clear</td>
                        <td>CLR</td>
                        <td>77</td>
                        <td>60</td>
                        <td> </td>
                        <td> </td>
                        <td>56%</td>
                        <td> </td>
                        <td>77</td>
                        <td>29.7</td>
                        <td>29.7</td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                    </tr>
                 
                                         <tr>
                        <td>Apr 27 17:53</td>
                        <td>ESE 8</td>
                        <td>10</td>
                        <td>Clear</td>
                        <td>CLR</td>
                        <td>79</td>
                        <td>59</td>
                        <td> </td>
                        <td> </td>
                        <td>50%</td>
                        <td> </td>
                        <td>79</td>
                        <td>29.71</td>
                        <td>29.71</td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                    </tr>
            </table>

        </div>
Link to comment
Share on other sites

that would be \n correct? I tried to but it doesn't work. just testing with 

<tr>
	<td>test</td>
	<td>test2</td>
</tr>

and 

<td>(.*)<\/td>\n<td>(.*)</td>

it returns nothing until I add \n\s and then It returns but when I add 17 

<td>(.*)<\/td>\n\s

together i doesn't work. 

 

I need everything between the <tr></tr> setup for an array to set for different variables. 

Link to comment
Share on other sites

after I remove all the format from the html code (no spaces or tabs) and using 

<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n<td>(.*)<\/td>\n

It works. 

 

but how would I use it with the spaces or tabs?

Link to comment
Share on other sites

There are better ways than regex, but for this I would suggest one regex to find the rows and another to find the TDs and run them in a loop. Note: Regex is not the fastest thing, so depending on how many records you have this could be an issue

 

//Create array to hold results
$results = array();
//Run regexx to find each row
$trPattern = "#<tr>(.*?)</tr>#is";
preg_match_all($trPattern, raw, $trMatches);
//Iterate over each row found
$trCount = 0;
foreach($trMatches[1] as $trText)
{
    //Run regexx to find each td
    $tdPattern = "#<td>(.*?)</td>#is";
    preg_match_all($tdPattern, $trText, $tdMatches);
    //Iterate over each td found
    $tdCount = 0;
    foreach($tdMatches[1] as $tdText)
    {
        //Append value to results
        $results[$trCount][$tdCount] = $tdText;
        //Increment td counter
        $tdCount++;
    }
    //Increment tr counter
    $trCount++;
}
 
echo "<pre>" . print_r($results, 1) . "</pre>";

 

Output

 

Array
(
[0] => Array
(
[0] => Apr 27 18:53
[1] => E 10
[2] => 10
[3] => Clear
[4] => CLR
[5] => 77
[6] => 60
[7] =>  
[8] =>  
[9] => 56%
[10] =>  
[11] => 77
[12] => 29.7
[13] => 29.7
[14] =>  
[15] =>  
[16] =>  
)

[1] => Array
(
[0] => Apr 27 17:53
[1] => ESE 8
[2] => 10
[3] => Clear
[4] => CLR
[5] => 79
[6] => 59
[7] =>  
[8] =>  
[9] => 50%
[10] =>  
[11] => 79
[12] => 29.71
[13] => 29.71
[14] =>  
[15] =>  
[16] =>  
)
 
)
Link to comment
Share on other sites

The example I gave doesn't use a regex at all. Am I not understanding what you want to do? Explain the what, not the how, as in how you think it should be done.

 

My understanding is you want the TD sets to be on its own line when viewing the source code. The foreach example I gave does exactly that.

Link to comment
Share on other sites

 

There are better ways than regex, but for this I would suggest one regex to find the rows and another to find the TDs and run them in a loop. Note: Regex is not the fastest thing, so depending on how many records you have this could be an issue

//Create array to hold results
$results = array();
//Run regexx to find each row
$trPattern = "#<tr>(.*?)</tr>#is";
preg_match_all($trPattern, raw, $trMatches);
//Iterate over each row found
$trCount = 0;
foreach($trMatches[1] as $trText)
{
    //Run regexx to find each td
    $tdPattern = "#<td>(.*?)</td>#is";
    preg_match_all($tdPattern, $trText, $tdMatches);
    //Iterate over each td found
    $tdCount = 0;
    foreach($tdMatches[1] as $tdText)
    {
        //Append value to results
        $results[$trCount][$tdCount] = $tdText;
        //Increment td counter
        $tdCount++;
    }
    //Increment tr counter
    $trCount++;
}
 
echo "<pre>" . print_r($results, 1) . "</pre>";

Output

Array
(
[0] => Array
(
[0] => Apr 27 18:53
[1] => E 10
[2] => 10
[3] => Clear
[4] => CLR
[5] => 77
[6] => 60
[7] =>  
[8] =>  
[9] => 56%
[10] =>  
[11] => 77
[12] => 29.7
[13] => 29.7
[14] =>  
[15] =>  
[16] =>  
)

[1] => Array
(
[0] => Apr 27 17:53
[1] => ESE 8
[2] => 10
[3] => Clear
[4] => CLR
[5] => 79
[6] => 59
[7] =>  
[8] =>  
[9] => 50%
[10] =>  
[11] => 79
[12] => 29.71
[13] => 29.71
[14] =>  
[15] =>  
[16] =>  
)
 
)

 

at the end of things I need to be able to set them as variables 

array_push($history_date, $m[1][$i]);

Now how to only use the 17 <td>'s and not all the td's in that webpage? How can I make sure it using only the information between the <div id="obs-site"> </div>?

 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.