Jump to content
#StayAtHome ×
branmh

preg_match_all

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>

Share this post


Link to post
Share on other sites

If your talking about what you see in view-source, all you need is the newline character after each closing TD

echo "<td>data</td>\n";
<?php
$arr = [1,2,3,4,5];
foreach($arr as $data)
{
 echo "<td>$data</td>\n";
}
?>
Edited by benanamen

Share this post


Link to post
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. 

Share this post


Link to post
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?

Share this post


Link to post
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] =>  
)
 
)

Share this post


Link to post
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.

Share this post


Link to post
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>?

 

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.