- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有没有一种方法可以获取所有具有部分匹配的 id 的元素。例如,如果我想抓取网页上所有 id 属性以 msg_
开头的 HTML 元素但在那之后可能是任何东西。
这是我到目前为止所做的:
$doc = new DomDocument;
// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('{URL IS HERE}'));
foreach($doc->getElementById('msg_') as $element) {
foreach($element->getElementsByTagName('a') as $link)
{
echo $link->nodeValue . "\n";
}
}
但我需要弄清楚如何用这个位进行部分 ID 匹配:$doc->getElementById('msg_')
或者如果有其他方法可以做到这一点...??
基本上,我需要获取 ID 以 msg_
开头的元素的子元素的所有“a”标签从技术上讲,永远只有 1 a
标签,但我不知道如何只捕获第一个 child ,这就是为什么我也在这个上使用 foreach。
DomDocument PHP 类可以做到这一点吗?
这是我现在使用的代码,它也不起作用:
$str = '';
$filename = 'http://dream-portal.net/index.php/board,65.0.html';
@set_time_limit(0);
$fp = fopen($filename, 'rb');
while (!feof($fp))
{
$str .= fgets($fp, 16384);
}
fclose($fp);
$doc = new DOMDocument();
$doc->loadXML($str);
$selector = new DOMXPath($doc);
$elements = $selector->query('//row[starts-with(@id, "msg_")]');
foreach ($elements as $node) {
var_dump($node->nodeValue) . PHP_EOL;
}
HTML如下(在span
标签中):
<td class="subject windowbg2">
<div>
<span id="msg_6555">
<a href="http://dream-portal.net/index.php?topic=834.0">Poll 1.0</a>
</span>
<p>
Started by
<a href="http://dream-portal.net/index.php?action=profile;u=1" title="View the profile of SoLoGHoST">SoLoGHoST</a>
<small id="pages6555">
«
<a class="navPages" href="http://dream-portal.net/index.php?topic=834.0">1</a>
<a class="navPages" href="http://dream-portal.net/index.php?topic=834.15">2</a>
»
</small>
with 963 Views
</p>
</div>
</td>
这是<span id="msg_
部分,其中有很多(HTML 页面上至少有 15 个)。
最佳答案
使用这个:
$str = file_get_contents('http://dream-portal.net/index.php/board,65.0.html');
$doc = new DOMDocument();
@$doc->loadHTML($str);
$selector = new DOMXPath($doc);
foreach ($selector->query('//*[starts-with(@id, "msg_")]') as $node) {
var_dump($node->nodeValue) . PHP_EOL;
}
给你:
string(8) "Poll 1.0"
string(12) "Shoutbox 2.2"
string(24) "Polaroid Attachments 1.6"
string(24) "Featured News Slider 1.3"
string(17) "Image Resizer 1.0"
string(8) "Blog 2.2"
string(13) "RSS Feeds 1.0"
string(19) "Adspace Manager 1.2"
string(21) "Facebook Like Box 1.0"
string(15) "Price Table 1.0"
string(13) "SMF Links 1.0"
string(19) "Download System 1.2"
string(16) "[*]Site News 1.0"
string(12) "Calendar 1.3"
string(16) "Page Peel Ad 1.1"
string(20) "Sexy Bookmarks 1.0.1"
string(15) "Forum Staff 1.2"
string(21) "Facebook Comments 1.0"
string(15) "Attachments 1.4"
string(25) "YouTube Channels 0.9 Beta"
关于PHP DomDocument - getElementByID(部分匹配)如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247943/
我有不同的 div,每个 div 都有一个调用相同函数的按钮。 . . . INIT . . . INIT 棘手的部分是每个按钮应该只对自己的 div 执行函数(#btn1 到 #div1、#
XSP.getElementById 和 document.getElementById 之间有什么区别?在我的测试中,两者似乎都返回相同的值(存储在字段中的值)。为 XPage 编码时应首选哪一个?
我通常会通过以下方式为某些事件注册 javascript 函数: myBtn.Attributes.Add("onClick", "Validate(getElementById('"+txtFirs
当我想检查页面中是否存在某个元素时。这两个检查是一样的吗?有没有更好更紧凑的方法来检查是否存在? 如果我想检查 value == '' 该怎么办。这也可以包含在这张支票中吗? 最佳答案 对元素的引用永
我尝试将新值分配给输入表单的隐藏输入和复选框。它在 Firefox 中工作正常,但在 IE 中则不然(我使用的是 IE 7)。有谁知道我的代码有什么问题吗? HTML: Javascript: v
我真的不知道如何描述我的问题,但基本上: 为了好玩,我在 JSFiddle 中编写了一个元素,不知何故我的代码输出只是上下跳动。 如果您想亲自查看,请单击下面的链接,然后单击“提交投诉”按钮。 问题是
document.getElementById("test").value document.getElementById("test").innerHTML 第一个表示地址,第二个表示存储在该地址的
在将 html block 插入 dom 之前,我对在 dom 外构建 html block 很感兴趣,因此我使用 dynatrace 进行了一些测试。我使用了bobince的方法: Is there
我在 GWT 应用程序中使用 native 函数,我尝试了这两种方法: document.getElementById("id") 返回 null 但 $doc.getElementById() 返回
以下代码有什么区别: Hover Over me SomeLink1 SomeLink2 SomeLink3
我正在尝试使用 javascript 设置 div 的内部 html,但由于某种原因,它不起作用。我发现其他人以前也遇到过这个问题,但我在其他帖子中找到的解决方案均无效。我不明白怎么了。 这是我的测试
编辑:我修复了分号、区分大小写以及方括号。如果我删除 buttonPARTICULAR 之后的函数,代码就可以工作!为什么? 编辑:固定。我是个笨蛋。对不起!!! :-Z 当我保持简单时,就像这样,一
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
我正在尝试创建一个通用的 JavaScript 函数来更改事件的属性。 它的工作方式是 function fooFunction(sourceElement) { var newName =
我需要获取元素的 ID,但该值是动态的,只有它的开头始终相同。 这是一段代码。 ID 总是以 poll- 开头,然后数字是动态的。 如何只使用 JavaScript 而不是 jQuery 获取 ID
我需要使用 VBA 从 HTML 中提取某些信息。 这是我试图单独提取位置信息的 HTML。 Location Dallas/Fort Worth Area Industry
我正在制作一个程序,该程序从输入字段返回值,然后根据条件将字段字符串更改为 x 结果。非常感谢您的帮助,因为这里的成员过去一直给我很大帮助。调试器抛出此错误,当然没有任何效果: script.js:2
我不确定为什么这不起作用,有人可以告诉我为什么吗? var red = [0, 100, 63]; var orange = [40, 100, 60]; var green = [75, 100,
这个问题已经有答案了: Javascript Shorthand for getElementById (24 个回答) 已关闭 8 年前。 我的下面的代码工作正常,但我想知道,是否有任何仅通过 Ja
我有这个simple HTML 标记: //a reserved property name id //same here 但是——运行: alert(document.getEle
我是一名优秀的程序员,十分优秀!