2017-01-16 110 views
0

我已经搜索并测试了几个小时,准备放弃。 我有一个HTML页面,现在会改变每一个,然后,它的结构是这样的....

PHP代码从html页面提取数据,包括标签

100 or so lines of HTML 
<div class="the start of the info I want"> 
500 lines of HTML that I want to extract 
<div class="end of the info I want"> 
more lines of HTML 

这是我的代码不工作,我所试过的一处。

<?php 
$data = file_get_contents('http://www.soemstupidsite.xyz'); 
$regex = '#<div class="the start of the info I want">(.*?)<div 
class="end of the info I want">#'; 
preg_match($regex,$data,$match); 
print_r($match); 
echo $match[1]; 
?> 

返回以下错误:
PHP公告:未定义抵消:1 /home/www/mycrapcode.php第7行

到底什么是我做错了什么?

+0

'的var_dump($比赛)'来看看它返回 –

+1

我假设的偏移误差是因为数组为空是什么的print_r($比赛);显示。 – DeathRox

+0

'回波$匹配[1];'这行抛*备注*,这是因为阵列'$ match'是空的。 –

回答

1
$regex = '/<div class="the start of the info I want">(.*?)<div 
class="end of the info I want">/s'; 
+0

哇,一个烂的“缺失。这解决了它。非常感谢。如果你曾经在澳大利亚内陆地区看过我,我欠你一个啤酒队友! – DeathRox

+0

确定男人,很高兴) –

0

请阅读一下关于正则表达式修饰符/标志here

你需要的标志,是s标志,所以你的选择将多条线路上工作。

与示例代码:

<?php 
$data = file_get_contents('http://www.soemstupidsite.xyz'); 
$regex = '#<div class="the start of the info I want">(.*?)<div class="end of the info I want">#s'; 
preg_match($regex,$data,$match); 
print_r($match); 
echo $match[1]; 
?> 

另外:正则表达式必须在1号线,否则将无法正常工作。

+0

正则表达式在粘贴时包在一行上。谢谢你对's'标志的解释。 Alexandr Malyita在6分钟内击败你,为我寻找半天的答案。如果你一直在我的路上,我也会找到你的啤酒! – DeathRox