-3

Need a help. I want to get the commented array vai php regex or something like that and insert into database. Anyone have any idea how can i will get that commented array vai php ? I really appreciate that . Thanks in advance :)

   <body>
        <!--Array
                (
                    [0] => Pagedale
                    [1] => 3,304.
                    [2] => 1.19
                    [3] => $28,480
                    [4] => 93.43%
                    [5] => 22.40%
                    [6] => 0.2640
                    [7] => 0.3410
                    [8] => 0.0000
                    [9] => 0.3500
                    [10] => $189,823
                    [11] => 6.83%
                    [12] => 8.363%
                    [13] => $1,090,378
                    [14] => 39.25%
                    [15] => 1
                    [16] => $2,434,084.00
                    [17] => $2,778,093
                    [18] => $2,540,416
                    [19] => Sales Tax
                    [20] => $1,090,378
                    [21] => Utility Tax
                    [22] => $471,471
                    [23] => Court Fines/Fees
                    [24] => $351,583
                    [25] => Parks & Recreation
                    [26] => $475,127
                    [27] => Police
                    [28] => $185,013
                    [29] => TIF Match
                    [30] => $60,153
                    [31] => $47,417
                    [32] => 7
                    [33] => Pagedale PD
                    [34] => 17
                    [35] => Pagedale
                    [36] => 12.66%
                    [37] => $90,758
                    [38] => $351,583
                    [39] => 1420 Ferguson Ave
                    [40] => Pagedale
                    [41] => MO
                    [42] => 63133
                    [43] => 314-726-1200
                    [44] => http://www.cityofpagedale.com
                    [45] => M-F (9-5)
                    [46] => The Board of Aldermen meet on the second Thursday of each month at 7:30pm
                    [47] => 1420 Ferguson Auditorium
                    [48] => 1
                )
                -->
<div>Hello world </div>
                </body>
                </html>
Raw Scripter
  • 113
  • 1
  • 12

1 Answers1

0

To locate comments within the HTML markup I would suggest that a regex is probably not the best method but you can accomplish the stated aim easily using DOMDocument and DOMXPath. There is, within XPath 2 a comment() function that you would use in the XPath query - viz: $xp->query('//node/comment()') etc

$strhtml='
    <html>
    <head>
        <title>duff html</title>
    </head>
    <body>
    <!--
        first of too many
    -->
    <!--Array
            (
                [0] => Pagedale
                [1] => 3,304.
                [2] => 1.19
                [3] => $28,480
                [4] => 93.43%
                [5] => 22.40%
                [6] => 0.2640
                [7] => 0.3410
                [8] => 0.0000
                [9] => 0.3500
                [10] => $189,823
                [11] => 6.83%
                [12] => 8.363%
                [13] => $1,090,378
                [14] => 39.25%
                [15] => 1
                [16] => $2,434,084.00
                [17] => $2,778,093
                [18] => $2,540,416
                [19] => Sales Tax
                [20] => $1,090,378
                [21] => Utility Tax
                [22] => $471,471
                [23] => Court Fines/Fees
                [24] => $351,583
                [25] => Parks & Recreation
                [26] => $475,127
                [27] => Police
                [28] => $185,013
                [29] => TIF Match
                [30] => $60,153
                [31] => $47,417
                [32] => 7
                [33] => Pagedale PD
                [34] => 17
                [35] => Pagedale
                [36] => 12.66%
                [37] => $90,758
                [38] => $351,583
                [39] => 1420 Ferguson Ave
                [40] => Pagedale
                [41] => MO
                [42] => 63133
                [43] => 314-726-1200
                [44] => http://www.cityofpagedale.com
                [45] => M-F (9-5)
                [46] => The Board of Aldermen meet on the second Thursday of each month at 7:30pm
                [47] => 1420 Ferguson Auditorium
                [48] => 1
            )
            -->
                <div>Hello world </div>

                <!-- last comment -->
                <div> Last div </div>
            </body>
           </html>';


    $dom=new DOMDocument;
    $dom->loadHTML( $strhtml );
    $xp=new DOMXPath( $dom );
    $col=$xp->query('/html/body/comment()');

    if( $col && $col->length > 0 ){
        $comments=[];
        foreach( $col as $comment ){
            $comments[]=$comment->nodeValue;
        }
        printf('<pre>%s</pre>',print_r($comments,true));
    }

Will output:

Array
(
    [0] => 
            first of too many

    [1] => Array
                (
                    [0] => Pagedale
                    [1] => 3,304.
                    [2] => 1.19
                    [3] => $28,480
                    [4] => 93.43%
                    [5] => 22.40%
                    [6] => 0.2640
                    [7] => 0.3410
                    [8] => 0.0000
                    [9] => 0.3500
                    [10] => $189,823
                    [11] => 6.83%
                    [12] => 8.363%
                    [13] => $1,090,378
                    [14] => 39.25%
                    [15] => 1
                    [16] => $2,434,084.00
                    [17] => $2,778,093
                    [18] => $2,540,416
                    [19] => Sales Tax
                    [20] => $1,090,378
                    [21] => Utility Tax
                    [22] => $471,471
                    [23] => Court Fines/Fees
                    [24] => $351,583
                    [25] => Parks & Recreation
                    [26] => $475,127
                    [27] => Police
                    [28] => $185,013
                    [29] => TIF Match
                    [30] => $60,153
                    [31] => $47,417
                    [32] => 7
                    [33] => Pagedale PD
                    [34] => 17
                    [35] => Pagedale
                    [36] => 12.66%
                    [37] => $90,758
                    [38] => $351,583
                    [39] => 1420 Ferguson Ave
                    [40] => Pagedale
                    [41] => MO
                    [42] => 63133
                    [43] => 314-726-1200
                    [44] => http://www.cityofpagedale.com
                    [45] => M-F (9-5)
                    [46] => The Board of Aldermen meet on the second Thursday of each month at 7:30pm
                    [47] => 1420 Ferguson Auditorium
                    [48] => 1
                )

    [2] =>  last comment 
)
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Thanks but when i try to save those html in a file and try to load it $strhtml = file_get_contents('str.html'); It show some errors. Warning: DOMDocument::loadHTML(): Tag header invalid in Entity, line: 97 in C:\xampp\htdocs\projects\regex\index.php on line 5 here's the line 5 $dom->loadHTML( $strhtml ); – Raw Scripter Jan 15 '19 at 09:00
  • please explain further - do you mean when you try to save the HTML comments to file? Or the entire HTML page ( YOUR html example is invalid HTML and will cause an error with DOMDocument generally )?? – Professor Abronsius Jan 15 '19 at 09:15