【发布时间】:2014-09-05 07:19:43
【问题描述】:
我尝试了大部分的正则表达式。但他们不为我工作.. 我需要删除所有html标签并返回值的正则表达式......在我的html文件中有以下html标签:输入文本,选择。
$file_string = file_get_contents('page_to_scrape.html');
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
$title_out = $title[1];
preg_match('/<option value="ELIT">(.*)<\/option>/i', $file_string, $keywords);
$keywords_out = $keywords[1];
preg_match('/<option value="MAS" selected="selected">(.*)<\/option>/i', $file_string, $ash);
$ash_s = $ash[1];
preg_match('/<input type="text" value="(.*)"/>/i', $file_string, $description);
$description_out = $description[1];
preg_match_all('/<li><a href="(.*)">(.*)<\/a><\/li>/i', $file_string, $links);
?>
<p><strong>Title:</strong> <?php echo $title_out; ?></p>
<p><strong>Name:</strong> <?php echo $keywords_out; ?></p>
<p><strong>TExtbox:</strong> <?php echo $description_out; ?></p>
<p><strong>Event:</strong> <?php echo $ash_s; ?></p>
<p><strong>Links:</strong> <em>(Name - Link)</em><br />
<?php
echo '<ol>';
for($i = 0; $i < count($links[1]); $i++) {
echo '<li>' . $links[2][$i] . ' - ' . $links[1][$i] . '</li>';
}
echo '</ol>';
?>
</p>
HTML 文件 这是标题
</ul>
<div class="field">
<label>Event:</label>
<select name="event" class="event">
<option value="MAS" selected="selected">Same</option>
<option value="ELIT">Same4</option>
<option value="IPC">Same3</option>
<option value="VLMW">Same2</option>
</select>
</div>
<div class="field">
<label class="sub">Surname:</label>
<input name="search[name]" value="Smith" type="text">
<br>
<label class="sub">First Name:</label>
<input name="search[firstname]" value="Alex" type="text">
<br>
</div>
</div>
</body>
</html>
【问题讨论】:
-
你能告诉我们你的尝试吗?
-
给我们一个简短的 html 页面示例,您希望 php 从中获取什么。
-
你试过strip_tags
-
请看我的代码和html文件
-
为什么不只是
file_get_contents并以明文形式回显。
标签: php regex web-scraping