php获取页面并切割页面div内容
亮点: 1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。 2、切割处理方法已经封装成一个方法,可以直接引用。 3、顺便加上的截
亮点:
1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。
2、切割处理方法已经封装成一个方法,可以直接引用。
3、顺便加上的截取。//getWebDiv('id="taglist"','http://www.cnblogs.com/Zjmainstay/tag/');
View Code
<span>php </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>); </span><span>function</span> getWebDiv(<span>$div_id</span>,<span>$url</span>=<span>false</span>,<span>$data</span>=<span>false</span><span>){ </span><span>if</span>(<span>$url</span> !== <span>false</span><span>){ </span><span>$data</span> = <span>file_get_contents</span>( <span>$url</span><span> ); } </span><span>$charset_pos</span> = <span>stripos</span>(<span>$data</span>,'charset'<span>); </span><span>if</span>(<span>$charset_pos</span><span>) { </span><span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=utf-8',<span>$charset_pos</span><span>)) { </span><span>$data</span> = <span>iconv</span>('utf-8','utf-8',<span>$data</span><span>); }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gb2312',<span>$charset_pos</span><span>)) { </span><span>$data</span> = <span>iconv</span>('gb2312','utf-8',<span>$data</span><span>); }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gbk',<span>$charset_pos</span><span>)) { </span><span>$data</span> = <span>iconv</span>('gbk','utf-8',<span>$data</span><span>); } } </span><span>preg_match_all</span>('/<div>$data,<span>$pre_matches</span>,PREG_OFFSET_CAPTURE); <span>//</span><span>获取所有div前缀</span> <span>preg_match_all</span>('/$data,<span>$suf_matches</span>,PREG_OFFSET_CAPTURE); <span>//</span><span>获取所有div后缀</span> <span>$hit</span> = <span>strpos</span>(<span>$data</span>,<span>$div_id</span><span>); </span><span>if</span>(<span>$hit</span> == -1) <span>return</span> <span>false</span>; <span>//</span><span>未命中</span> <span>$divs</span> = <span>array</span>(); <span>//</span><span>合并所有div</span> <span>foreach</span>(<span>$pre_matches</span>[0] <span>as</span> <span>$index</span>=><span>$pre_div</span><span>){ </span><span>$divs</span>[(int)<span>$pre_div</span>[1]] = 'p'<span>; </span><span>$divs</span>[(int)<span>$suf_matches</span>[0][<span>$index</span>][1]] = 's'<span>; } </span><span>//</span><span>对div进行排序</span> <span>$sort</span> = <span>array_keys</span>(<span>$divs</span><span>); </span><span>asort</span>(<span>$sort</span><span>); </span><span>$count</span> = <span>count</span>(<span>$pre_matches</span>[0<span>]); </span><span>foreach</span>(<span>$pre_matches</span>[0] <span>as</span> <span>$index</span>=><span>$pre_div</span><span>){ </span><span>//</span><span><div> <span>if</span>((<span>$pre_matches</span>[0][<span>$index</span>][1] $hit) && (<span>$hit</span> $pre_matches[0][<span>$index</span>+1][1<span>])){ </span><span>$deeper</span> = 0<span>; </span><span>//</span><span>弹出被命中div前的div</span> <span>while</span>(<span>array_shift</span>(<span>$sort</span>) != <span>$pre_matches</span>[0][<span>$index</span>][1] && (<span>$count</span>--)) <span>continue</span><span>; </span><span>//</span><span>对剩余div进行匹配,若下一个为前缀,则向下一层,$deeper加1, //否则后退一层,$deeper减1,$deeper为0则命中匹配,计算div长度</span> <span>foreach</span>(<span>$sort</span> <span>as</span> <span>$key</span><span>){ </span><span>if</span>(<span>$divs</span>[<span>$key</span>] == 'p') <span>$deeper</span>++<span>; </span><span>else</span> <span>if</span>(<span>$deeper</span> == 0<span>) { </span><span>$length</span> = <span>$key</span>-<span>$pre_matches</span>[0][<span>$index</span>][1<span>]; </span><span>break</span><span>; }</span><span>else</span><span> { </span><span>$deeper</span>--<span>; } } </span><span>$hitDivString</span> = <span>substr</span>(<span>$data</span>,<span>$pre_matches</span>[0][<span>$index</span>][1],<span>$length</span>).'</div>'<span>; </span><span>break</span><span>; } } </span><span>return</span> <span>$hitDivString</span><span>; } </span><span>echo</span> getWebDiv('id="taglist"','http://www.cnblogs.com/Zjmainstay/tag/'<span>); </span><span>//</span><span>End_php</span> <p>考虑到id符号问题,id="u"由用户自己填写。</p> <p>声明:此段php只针对带 id div内容的读取。</p> <p><img src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnblogs.com%2FOutliningIndicators%2FContractedBlock.gif&refer=http%3A%2F%2Fwww.cnblogs.com%2FZjmainstay%2Farchive%2F2012%2F08%2F06%2Fphp_getDivContain.html" class="lazy" alt="php获取页面并切割页面div内容" ><img src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnblogs.com%2FOutliningIndicators%2FExpandedBlockStart.gif&refer=http%3A%2F%2Fwww.cnblogs.com%2FZjmainstay%2Farchive%2F2012%2F08%2F06%2Fphp_getDivContain.html" class="lazy" alt="php获取页面并切割页面div内容" ><span>View Code </span> </p> <p> </p> <pre class="brush:php;toolbar:false"><span> 1</span> <span>php </span><span> 2</span> <span>header</span>("Content-type: text/html; charset=utf-8"<span>); </span><span> 3</span> <span>function</span> getWebTag(<span>$tag_id</span>,<span>$url</span>=<span>false</span>,<span>$tag</span>='div',<span>$data</span>=<span>false</span><span>){ </span><span> 4</span> <span>if</span>(<span>$url</span> !== <span>false</span><span>){ </span><span> 5</span> <span>$data</span> = <span>file_get_contents</span>( <span>$url</span><span> ); </span><span> 6</span> <span> } </span><span> 7</span> <span>$charset_pos</span> = <span>stripos</span>(<span>$data</span>,'charset'<span>); </span><span> 8</span> <span>if</span>(<span>$charset_pos</span><span>) { </span><span> 9</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=utf-8',<span>$charset_pos</span><span>)) { </span><span>10</span> <span>$data</span> = <span>iconv</span>('utf-8','utf-8',<span>$data</span><span>); </span><span>11</span> }<span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gb2312',<span>$charset_pos</span><span>)) { </span><span>12</span> <span>$data</span> = <span>iconv</span>('gb2312','utf-8',<span>$data</span><span>); </span><span>13</span> }<span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gbk',<span>$charset_pos</span><span>)) { </span><span>14</span> <span>$data</span> = <span>iconv</span>('gbk','utf-8',<span>$data</span><span>); </span><span>15</span> <span> } </span><span>16</span> <span> } </span><span>17</span> <span>18</span> <span>preg_match_all</span>('/$tag
修复:stripos($data,'charset=utf-8',$charset_pos) 加入charset=,避免有些gb2312格式的网页中包含utf-8造成错误。或者用户可以自行修改函数传入一个确定的charset参数。
演示地址:parseDiv

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Dans ce chapitre, nous comprendrons les variables d'environnement, la configuration générale, la configuration de la base de données et la configuration de la messagerie dans CakePHP.

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Pour travailler avec la date et l'heure dans cakephp4, nous allons utiliser la classe FrozenTime disponible.

Pour travailler sur le téléchargement de fichiers, nous allons utiliser l'assistant de formulaire. Voici un exemple de téléchargement de fichiers.

Dans ce chapitre, nous allons apprendre les sujets suivants liés au routage ?

CakePHP est un framework open source pour PHP. Il vise à faciliter grandement le développement, le déploiement et la maintenance d'applications. CakePHP est basé sur une architecture de type MVC à la fois puissante et facile à appréhender. Modèles, vues et contrôleurs gu

Le validateur peut être créé en ajoutant les deux lignes suivantes dans le contrôleur.

Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c
