问题描述
我要访问一个自定义属性,我添加到一个HTML文件中的某些元素,这里的 littleBox =someValue中
属性的示例
< DIV ID =someIdlittleBox =someValue中>内文字< / DIV>
以下不工作:
的foreach($ HTML的>找到('DIV')为$元素){
回声$元素;
如果(使用isset($元素 - >类型)){
回声$元素 - > littleBox;
}
}
我看到了类似的问题的文章,但我不能复制它的某些原因。这里是我的尝试:
函数retrieveValue($ STR){
如果(stripos函数($海峡,littleBox')){//检查是否元素有它
是$ var = preg_split(/ littleBox = \\/,$海峡);
//回声是$ var [1];
$ VAR1 = preg_split(/ \\/,是$ var [1]);
回声$ VAR1 [0];
}
其他
返回false;
}
在以往我称之为 retrieveValue()
功能,没有任何反应。为 $元素
(上述第一个PHP示例)不是一个字符串?我不知道,但如果我错过了一些东西它不会返回任何东西。
下面的脚本在它的全部内容:
< PHP
需要(../../ simplehtmldom / simple_html_dom.php);如果(使用isset($ _ POST ['提交'])){$ HTML = file_get_html($ _ POST ['WEBURL']);//查找所有图片
的foreach($ HTML的>找到('DIV')为$元素){
回声$元素;
如果(使用isset($元素 - >!型)= FALSE){
回声retrieveValue($元素);
}
}
}
功能retrieveValue($ STR){
如果(stripos函数($海峡,littleBox')){//检查是否元素有它
是$ var = preg_split(/ littleBox = \\/,$海峡);
//回声是$ var [1];
$ VAR1 = preg_split(/ \\/,是$ var [1]);
返回$ VAR1 [0];
}
其他
返回false;
}?><形式方法=邮报>
网站网址<输入类型=文本名称=WEBURL>
< BR />
<输入类型=提交名称=提交>
< /表及GT;
你试过:
$ HTML的>的getElementById(someId) - >的getAttribute('littleBox');
您也可以使用SimpleXML的:
$的HTML ='< DIV ID =someIdlittleBox =someValue中>内文字< / DIV>';
$ DOM =新的DOM文档;
$ dom->的loadXML($ HTML);
$ DIV = simplexml_import_dom($ DOM);
回声$ div->属性() - GT; littleBox;
我会建议不要使用正则表达式来解析HTML ,但不应该这部分是这样的:
$海峡= $ HTML的>的getElementById(someId) - > outertext;
是$ var = preg_split('/ littleBox = \\/',$海峡);
$ VAR1 = preg_split('/ \\/,是$ var [1]);
回声$ VAR1 [0];
另见这个答案
I want to access a custom attribute that I added to some elements in an HTML file, here's an example of the littleBox="somevalue"
attribute
<div id="someId" littleBox="someValue">inner text</div>
The Following doesn't work:
foreach($html->find('div') as $element){
echo $element;
if(isset($element->type)){
echo $element->littleBox;
}
}
I saw an article with a similar problem, but I couldn't replicate it for some reason. Here is what I tried:
function retrieveValue($str){
if (stripos($str, 'littleBox')){//check if element has it
$var=preg_split("/littleBox=\"/",$str);
//echo $var[1];
$var1=preg_split("/\"/",$var[1]);
echo $var1[0];
}
else
return false;
}
When ever I call the retrieveValue()
function, nothing happens. Is $element
(in the first PHP example above) not a string? I don't know if I missed something but it's not returning anything.
Here's the script in it's entirety:
<?php
require("../../simplehtmldom/simple_html_dom.php");
if (isset($_POST['submit'])){
$html = file_get_html($_POST['webURL']);
// Find all images
foreach($html->find('div') as $element){
echo $element;
if(isset($element->type)!= false){
echo retrieveValue($element);
}
}
}
function retrieveValue($str){
if (stripos($str, 'littleBox')){//check if element has it
$var=preg_split("/littleBox=\"/",$str);
//echo $var[1];
$var1=preg_split("/\"/",$var[1]);
return $var1[0];
}
else
return false;
}
?>
<form method="post">
Website URL<input type="text" name="webURL">
<br />
<input type="submit" name="submit">
</form>
Have you tried:
$html->getElementById("someId")->getAttribute('littleBox');
You could also use SimpleXML:
$html = '<div id="someId" littleBox="someValue">inner text</div>';
$dom = new DOMDocument;
$dom->loadXML($html);
$div = simplexml_import_dom($dom);
echo $div->attributes()->littleBox;
I would advice against using regex to parse html but shouldn't this part be like this:
$str = $html->getElementById("someId")->outertext;
$var = preg_split('/littleBox=\"/', $str);
$var1 = preg_split('/\"/',$var[1]);
echo $var1[0];
Also see this answer http://stackoverflow.com/a/8851091/1059001
这篇关于PHP简单的HTML DOM解析器:访问自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!