更新:到目前为止,这段代码工作正常。还在推特.
$trimmed = file('http://www.edvizenor.com/mercy/love/dm8.php');
foreach ($trimmed as $line_num => $line)
{
if(preg_match("/<td class=\"num\">/", $trimmed[$line_num], $matches))
{
$num++;
}
$Content[$num] .= $trimmed[$line_num];
}
// Example array!
echo $Content[6];/
结束更新
我正在尝试使用php将表内容放到数组中。我想把每个数字之后的所有段落放入一个数组中。
这是网址:http://edvizenor.com/mercy/love/dm8.php
我需要将每个段落编号的文本内容放到一个数组中,以及段落号。一旦我有了它,我就想把它们保存在一个文件夹中,放到一个由段落号命名的txt文件中。
例如,在我将内容放入正确的数组之后,如果我想调用第832段,那么我将执行如下操作:
$par = file_get_contents("353.txt");
// Need code to get only number
echo $num;
echo "<br>";
echo $par;
// should echo out:
353
When Mother left for the chapel and I stayed to set the room in order,
I heard these words: Tell all the sisters that I demand that they live
in the spirit of faith towards the superiors at this present time. I
begged my confessor to release me from this duty. 我想把整个内容写成一个字符串,然后“爆炸”;
$Content = file_get_contents("http://edvizenor.com/mercy/love/dm8.php");
$ContentArray = explode("<td class=\"num\">", $Content);但事情变得越来越复杂了。所以我阻止了它。
任何关于最好的方法的想法。我一共有1868年的段落和内容。所以复制和粘贴将是一项很大的工作。谢天谢地,所有的html都是相同的,所以我应该能够找到一个模式,并相应地做我想做的事情。但是我想不出答案,所以我在StackOverflow上找出了明智的答案:)
发布于 2012-09-11 02:28:58
其思想是将url的内容捕获到变量中,然后使用domxpath查询它并将其循环到数组中。
$dom = new DOMDocument();
@$dom -> loadHTML($html);
$xpath = new DOMXPath($dom);
$nums = $xpath->query("//td[@class='num']");
$paragraphs = $xpath->query("//td[@class='num']/following::td[1]");
for($j = 0; $j < $nums->length; $j++){
$num = $nums->item($j)->nodeValue;
$para = $paragraphs->item($j)->nodeValue;
$para_array[$num] = $para;
}这是一个示例结果。http://randtest.site11.com/
https://stackoverflow.com/questions/12361743
复制相似问题