查找并存储div的所有innerhtml到php变量 [英] Find and store all innerhtml of div to php variable

查看:131
本文介绍了查找并存储div的所有innerhtml到php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将div的内容存储到变量中.

I’m trying to store the content of a div to a variable.

示例:

  <div class="anything">
    <p>We don't know the content of this div</p>
    </div>

我想搜索<div class="anything">并将所有内容存储在开始标记和结束标记之间.

I want to search for <div class="anything"> and store everything between opening and the end tag.

我们还希望避免使用绝对路径名,以便仅在当前HTML/PHP文件中查找存在代码的div.

We also want to avoid using absolute pathnames, so that it only searches the current HTML/PHP file for this div where the code is present.

这在PHP中是可能的,还是仅在JavaScript中才可能?

Is this possible with PHP, or is this only possible with JavaScript ?

推荐答案

PHP不那么聪明.他甚至都不知道他在说什么.

PHP是服务器-辅助语言.它在交付页面时绝对不知道DOM是什么(即,浏览器窗口中显示的内容).是的,我知道,PHP 呈现了该DOM,所以它怎么可能不知道其中有什么?

PHP is not that intelligent. He doesn't even know what he says.

PHP is a server-side language. It has absolutely NO clue about what the DOM (ie. what is displayed in your browser's window) is when it delivers a page. Yeah I know, PHP rendered the DOM, so how could it not know what's in there?

简而言之,假设 PHP不存储他提供的内容.他只知道在某个特定时刻,他正在交付字符串,但这仅此而已.他的种类没什么大不了的.全局到达客户端,称为DOM.服务器(PHP)在渲染时会立即将其忘记.

Simply put, let's say that PHP doesn't have a memory of what he renders. He just knows that at one particular moment, he is delivering strings of characters, but that's all. He kind of doesn't get the big picture. The big picture goes to the client and is called the DOM. The server (PHP) forgets it immediately as he's rendering it.

就像一条红色的鱼.

为此,您需要JavaScript(位于客户端计算机上,因此可以完全访问呈现的DOM),或者,如果您希望PHP执行此操作,则必须检索完整呈现的页面.

To do that, you need JavaScript (which is on the client's computer, and therefore has complete access to the rendered DOM), or if you want PHP to do this, you have to retrieve an full-rendered page first.

因此,用PHP完成您要执行的操作的唯一方法是打印页面,然后才能使用http请求进行检索,并使用 simpleHtmlDom .

So the only way to do what you want to do in PHP is to get your page printed, and only then you can retrieve it with an http request and parse it with, in your case, a library such as simpleHtmlDom.

假设您知道您的页面将在 http://mypage.com/mypage.php

Let's say you know that your page will be available at http://mypage.com/mypage.php

$html = file_get_html('http://mypage.com/mypage.php');

foreach($html->find('div.anything') as $element) 
    echo $element->src . '<br>';

这篇关于查找并存储div的所有innerhtml到php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆