PHP 根据 HTML 表解析文件中的另一个值查找和获取值
PHP find and get value based on another one from HTML table parsed file
我正在为我的项目使用PHP Simple HTML DOM解析器。
我正在尝试查找特定数据并在从包含 HTML 表元素中的数据的 URL 网站解析我的 .php 文件后回显它,示例如下:
<table class="example">
<tbody>
<tr>
<td>
Heading #1
<p>Description of heading #1 here ...</p>
</td>
<td>Example of data #1</td>
</tr>
<tr>
<td>
Heading #2
<p>Description of heading #2 here ...</p>
</td>
<td>Example of data #2</td>
</tr>
</tbody>
</table>
我的问题:
如何通过知道同一 TR 行中的第一个 TD 单元格包含值"标题 #1..."从这种桌子?
我已经解析了URL,现在我需要根据它旁边的另一个值找到值。
我应该使用一些正则表达式并为此制作一些模式吗? strpos() 和数组?
你需要给表分区一个 JavaScript 的 ID,以便能够获取要提交的数据,并将其放入带有名称和 ID 的隐藏输入中,以便 PHP 使用 POST 获取它们。
<script language="javascript">
function transfer_data(){
documentGetElementById('ex1_hidden').value = documentGetElementById('ex1').innerHTML;
documentGetElementById('ex2_hidden').value = documentGetElementById('ex2').innerHTML;
submit();
}
</script>
<table class="example">
<tbody>
<tr>
<td id="hdg1">
Heading #1
<p>Description of heading #1 here ...</p>
</td>
<td id="ex1">Example of data #1</td>
</tr>
<tr>
<td>
Heading #2
<p>Description of heading #2 here ...</p>
</td>
<td id="ex2">Example of data #2</td>
</tr>
</tbody>
</table>
在您的表格中,使用您需要的method="post"
提交到您希望它去的任何地方:
<input type="hidden" name="ex1_hidden" id="ex1_hidden" />
<input type="hidden" name="ex2_hidden" id="ex2_hidden" />
<input type="button" value="Submit" onClick="transfer_data()" />
在PHP中,你会用$_POST['ex1_hidden']
和$_POST['ex2_hidden']
来拿起它们(记得清理提交的数据)。
这不是适合安全数据的方法。
您可以向标题添加 ID,并在脚本中使其成为条件:
if(documentGetElementById('hdg1').innerHTML == "Heading #1"){
documentGetElementById('ex1_hidden').value = documentGetElementById('ex1').innerHTML;
}
您可能需要使用类似的东西修剪标题上的空格
var str=documentGetElementById('hdg1').innerHTML.replace(/^'s+|'s+$/g,'');
关于使用 jQuery 抓取文本时如何去除空格的信用@Paul?
这里有很多关于其他方式的有用想法 如何使用jQuery获取表单元格值?
如果这是从另一个网站抓取的数据,你根本无法控制,但你已经在PHP变量中拥有,你可以通过<td>
来explode()
它,并计算出哪些数组位置包含你想要的数据。参考: http://php.net/manual/en/function.explode.php
这就是我认为您真正正在寻找的 - 首先询问网站所有者是否可以使用可能是个好主意,但这取决于您。您在使用strpos();
和数组(使用您的表进行测试)方面走在正确的轨道上:
// only works if fopen is allowed on the site's server and in PHP5+
$handle = fopen("http://websiteyouwanttoscrape.com/file.html", "r");
$contents = stream_get_contents($handle);
$contents_array = array();
$bit_i_want = array();
// give yourself a chance
$contents = htmlspecialchars($contents);
// swap these if you don't use htmlspecialchars();
$contents_array = explode('<td>',$contents);
//$contents_array = explode('<td>',$contents);
$counter = 0;
while($counter < count($contents_array)){
if(strpos($contents_array[$counter], 'Heading #1') > 0 ){
// swap these if you don't use htmlspecialchars();
$bit_i_want = explode('</td>',$contents_array[$counter+1]);
//$bit_i_want = explode('</td>',$contents_array[$counter+1]);
echo $bit_i_want[0] . '<br />';
// uncomment break; to stop the loop if you don't
// want to look for any more instances of "Heading #1" if there were any
//break;
}
$counter++;
}
fclose($handle); //close the file
- 在另一个Find(..)的回调内部查找,如何逃离回调地狱
- 如果我知道另一个值,请在多维数组中查找一个值
- 从另一个域中查找视频位置宽度视频
- 如何在另一个页面中查找元素,并从当前页面上的元素中输入值
- 科尔多瓦使用SplashScreen插件在另一个文件夹中查找cordova_plugins.js
- 如何在一个窗体上创建子网格并将其值传递给另一个窗体中的查找
- 如何查找数组中另一个对象旁边的对象
- 查找一个对象是否是 javascript 中另一个对象的子集
- 从四个输入字段中查找总计或平均值,并在另一个输入字段中显示值
- 如何告诉 JSHint 在另一个 JS 文件中查找定义
- MONGO 查找数组中至少有一个项目不在另一个项目中的文档
- 通过与另一个对象的部分匹配来查找对象
- 查找字符串的单个匹配项,然后查找另一个模式的多个匹配项
- 正则表达式:查找特定字符串,只要它出现在另一个特定字符串之前
- 使用 PHP 查找并删除一些 javascript 标签,但保留另一个标签
- PHP 根据 HTML 表解析文件中的另一个值查找和获取值
- jquery根据输入值按名称查找元素,并将其id分配给另一个元素
- 查找不包含在另一个元素中的元素的有效方法
- 在对象数组中查找特定的键/值,然后打印出该对象中的另一个键/值
- 正则表达式,用于查找一个术语之后和另一个术语之前的单词