branmh Posted April 28, 2017 Share Posted April 28, 2017 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> Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/ Share on other sites More sharing options...
benanamen Posted April 28, 2017 Share Posted April 28, 2017 (edited) 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 April 28, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/#findComment-1545942 Share on other sites More sharing options...
branmh Posted April 28, 2017 Author Share Posted April 28, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/#findComment-1545943 Share on other sites More sharing options...
branmh Posted April 28, 2017 Author Share Posted April 28, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/#findComment-1545944 Share on other sites More sharing options...
Psycho Posted April 28, 2017 Share Posted April 28, 2017 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] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/#findComment-1545947 Share on other sites More sharing options...
benanamen Posted April 28, 2017 Share Posted April 28, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/#findComment-1545948 Share on other sites More sharing options...
branmh Posted April 28, 2017 Author Share Posted April 28, 2017 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>? Quote Link to comment https://forums.phpfreaks.com/topic/303819-preg_match_all/#findComment-1545958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.