{"id":5668,"date":"2019-04-08T09:52:37","date_gmt":"2019-04-08T01:52:37","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=5668"},"modified":"2020-05-24T09:04:31","modified_gmt":"2020-05-24T01:04:31","slug":"phpquery-%e8%ae%93-php-%e5%85%a7%e7%9a%84-html-%e6%b6%88%e5%a4%b1","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=5668","title":{"rendered":"phpQuery - \u8b93 PHP \u5167\u7684 HTML \u6d88\u5931"},"content":{"rendered":"<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/code.google.com\/archive\/p\/phpquery\/\" target=\"_blank\" rel=\"noopener\">phpquery<\/a><\/li>\n<li><a href=\"https:\/\/blog.johnsonlu.org\/phpphpquery\/\" target=\"_blank\" rel=\"noopener\">[PHP]phpQuery \u2013 \u4f5b\u7956\u7403\u7403<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<p>\u5982\u679c<span style=\"color: #333333; font-style: normal; font-weight: 300;\">\u4f60<\/span><span style=\"font-weight: 300;\">\u4ee5\u5f8c\u4e0d\u60f3\u5beb\u50cf\u4e0b\u9762\u9019\u7a2e HTML, PHP \u6df7\u5728\u4e00\u8d77\u7684\u721b\u7a0b\u5f0f\uff0c\u8acb\u7e7c\u7e8c\u5f80\u4e0b\u770b\uff0c\u5426\u5247\u53ef\u4ee5\u95dc\u6389\u7db2\u9801\u4e86<\/span><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5224\u65b7\u662f\u5426\u6709\u767b\u5165\u6703\u54e1\u4e4b\u5f8c\u986f\u793a\u4e0d\u540c\u5167\u5bb9\u7684\u7db2\u9801<\/p>\n<pre class=\"lang:php decode:true\">&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;?php\r\nif ( $_SESSION['Login'] !='Y' ) {\r\n    ?&gt;\r\n    &lt;div&gt;&lt;a href=\"Login.php\"&gt;\u767b\u5165\u6703\u54e1&lt;\/a&gt;&lt;\/div&gt;\r\n&lt;?php\r\n}\r\n\r\nelse {\r\n    ?&gt;\r\n    &lt;div&gt;&lt;?php echo $Username;?&gt;&lt;\/div&gt;\r\n\r\n    &lt;hr\/&gt;\r\n\r\n    &lt;div&gt;\u6703\u54e1\u8cc7\u8a0a.1&lt;\/div&gt;\r\n    &lt;div&gt;\u6703\u54e1\u8cc7\u8a0a.2&lt;\/div&gt;\r\n    &lt;div&gt;\u6703\u54e1\u8cc7\u8a0a.3&lt;\/div&gt;\r\n&lt;?php\r\n}\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>--<\/p>\n<h2>\u5c07\u6240\u6709\u5167\u5bb9\u8a2d\u8a08\u5728\u524d\u7aef HTML \u5167<\/h2>\n<p>\u70ba\u4e86\u8b93 PHP \u6efe\u51fa HTML\uff0c\u9019\u88e1\u4e0d\u4f7f\u7528\u5176\u4ed6 PHP \u6a21\u677f\uff0c\u56e0\u70ba\u90a3\u53ea\u662f\u53e6\u4e00\u7a2e\u6df7\u7528\u7684\u65b9\u5f0f\uff0c\u800c\u4e14\u6a21\u677f\u4e0d\u540c\u8a9e\u6cd5\u9084\u4e0d\u540c\u53c8\u7522\u751f\u4e86\u65b0\u7684\u554f\u984c<\/p>\n<p>\u76f4\u63a5\u8b93\u524d\u7aef\u8a2d\u8a08\u4e00\u500b\u53ef\u4ee5\u5207\u63db\u986f\u793a\u7d50\u679c\u7684\u6a5f\u5236\uff0c\u7136\u5f8c\u5c07\u5207\u63db\u7684\u65b9\u5f0f\u4ea4\u7d66 PHP \u5373\u53ef\uff0c\u4f8b\u5982\u4ee5\u4e0b\u7684\u5207\u63db class \u986f\u793a\u63a7\u5236\u65b9\u5f0f<\/p>\n<p>\u4f7f\u7528 class=\"Login\" \u5224\u65b7\u662f\u767b\u5165\u624d\u53ef\u4ee5\u986f\u793a\u5167\u5bb9\uff1bclass=\"Logout\" \u662f\u672a\u767b\u5165\u986f\u793a\u5167\u5bb9\uff0c\u672a\u767b\u5165\u6642\u5c07\u6240\u6709\u5df2\u767b\u5165\u5167\u5bb9\u79fb\u9664\uff0c\u767b\u5165\u5f8c\u53cd\u4e4b<\/p>\n<pre class=\"lang:default decode:true\">&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;script src=\"\/3rdParty\/jquery-1.11.3.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"Logout\"&gt;&lt;a href=\"Login.php\"&gt;\u767b\u5165\u6703\u54e1&lt;\/a&gt;&lt;\/div&gt;\r\n&lt;div class=\"Login\"&gt;{{UserName}}&lt;\/div&gt;\r\n\r\n&lt;hr class=\"Login\"\/&gt;\r\n\r\n&lt;div class=\"Login\"&gt;\u6703\u54e1\u8cc7\u8a0a.1&lt;\/div&gt;\r\n&lt;div class=\"Login\"&gt;\u6703\u54e1\u8cc7\u8a0a.2&lt;\/div&gt;\r\n&lt;div class=\"Login\"&gt;\u6703\u54e1\u8cc7\u8a0a.3&lt;\/div&gt;\r\n\r\n&lt;script&gt;\r\n    $('.Login').remove();\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\u53ea\u8981\u63a7\u5236 remove() \u5c0d\u8c61\u5373\u53ef\u5207\u63db\u662f\u5426\u767b\u5165\u6703\u54e1\u5167\u5bb9\uff0c\u63a5\u4e0b\u4f86\u5c31\u662f\u5c07 jQuery \u7684\u52d5\u4f5c\u4ea4\u7d66 phpQuery \u4f86\u505a<\/p>\n<p>--<\/p>\n<h2>\u8b93 PHP \u63a7\u5236 DOM<\/h2>\n<p>\u4ee5\u4e0a\u9762\u7684\u4f8b\u5b50\u70ba\u4f8b\uff0c\u53ef\u4ee5\u79fb\u9664\u5e95\u4e0b\u7684 JavaScript \u63a7\u5236\uff0c\u6e96\u5099\u8b93 PHP \u4f86\u505a\u76f8\u540c\u7684\u4e8b\u60c5<\/p>\n<pre class=\"lang:default decode:true \">&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"Logout\"&gt;&lt;a href=\"Login.php\"&gt;\u767b\u5165\u6703\u54e1&lt;\/a&gt;&lt;\/div&gt;\r\n&lt;div class=\"Login\"&gt;{{UserName}}&lt;\/div&gt;\r\n\r\n&lt;hr class=\"Login\"\/&gt;\r\n\r\n&lt;div class=\"Login\"&gt;\u6703\u54e1\u8cc7\u8a0a.1&lt;\/div&gt;\r\n&lt;div class=\"Login\"&gt;\u6703\u54e1\u8cc7\u8a0a.2&lt;\/div&gt;\r\n&lt;div class=\"Login\"&gt;\u6703\u54e1\u8cc7\u8a0a.3&lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>PHP \u4ee3\u78bc<\/p>\n<p>\u5c07 HTML \u8b80\u5165\u4e4b\u5f8c\uff0c\u6839\u64da session \u7d50\u679c\u5957\u7528\u548c jQuery \u5c0d\u61c9\u529f\u80fd<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nrequire_once '\/www\/phpQuery-onefile.php';\r\n\r\n$html = file_get_contents('\/www\/index.html');\r\n$html = str_replace('{{webRoot}}', webRoot, $html);\r\n\r\n$dom = phpQuery::newDocumentHTML($html);\r\n\r\n\/\/ \u5df2\u767b\u5165\r\nif ( $_SESSION['Login'] =='Y' ){\r\n    $dom-&gt;find('.Logout')-&gt;remove();\r\n    $dom = str_replace('{{UserName}}', 'Hoyo', $dom);\r\n}\r\n\r\n\/\/ \u672a\u767b\u5165\r\nelse{\r\n    $dom-&gt;find('.Login')-&gt;remove();\r\n}\r\n\r\necho $dom;<\/pre>\n<p>\u4f7f\u7528 phpQuery \u5373\u53ef\u8b93\u524d\u7aef HTML \u64c1\u6709\u5b8c\u5168\u63a7\u5236\u7db2\u9801\u8f38\u51fa\u5167\u5bb9\u7684\u80fd\u529b<\/p>\n<p>--<\/p>\n<h2>checkbox<\/h2>\n<pre class=\"lang:php decode:true\">if ($gmail_alarm_enable =='Y'){\r\n    $dom-&gt;find('#id_gmail_alarm_enable')-&gt;attr('checked', '');\r\n}\r\nelse{\r\n    else $dom-&gt;find('#id_gmail_alarm_enable')-&gt;removeAttr('checked');\r\n}<\/pre>\n<p>--<\/p>\n<h2>select<\/h2>\n<pre class=\"lang:php decode:true\">$dom-&gt;find('#id_tou')-&gt;val($tou);<\/pre>\n<p>--<\/p>\n<ul>\n<li><a href=\"https:\/\/www.itdaan.com\/blog\/2014\/07\/17\/6ad580ce8d65d6092159d1ee09f39e67.html\" target=\"_blank\" rel=\"noopener\">\u4f7f\u7528phpQuery\u7372\u53d6\u6578\u7d44<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"5668\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,923&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u8cc7\u6e90 phpquery [...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"5668\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,923&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/5668"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5668"}],"version-history":[{"count":10,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/5668\/revisions"}],"predecessor-version":[{"id":7022,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/5668\/revisions\/7022"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}