下面是我要搜索的文本值,这个文本中没有ant HTML (这是HTML DOM无法工作的问题)。
User Guide
For iOS 7.1 Software
Contents
Chapter 1: New One
iPhone at a Glance
iPhone
overview
Accessories
Multi-Touch screen
Buttons
Status icons
Chapter 2 Second One this is long
Chapter 3 new this is long好的,现在我试图得到Chapter 1: New One和Chapter 2 Second One this is long类型的值,还有更多的章节要做。
我尝试了PHP,但不知道如何使用different format和length提取这些章节。
发布于 2014-07-08 13:33:22
你是说像这样的事.?
<?php
$lines = " User Guide
For iOS 7.1 Software
Contents
Chapter 1: New One
iPhone at a Glance
iPhone
overview
Accessories
Multi-Touch screen
Buttons
Status icons
Chapter 2 Second One this is long
Chapter 3 new this is long";
$lines = explode("\r\n", $lines);
foreach ($lines as $line) {
$line = trim($line);
if (!empty($line)) {
if (preg_match('/Chapter \\d/', $line)) {
echo $line ."<br>";
}
}
}产出:
Chapter 1: New One
Chapter 2 Second One this is long
Chapter 3 new this is long发布于 2014-07-08 13:36:41
没有DOM,所以使用方法对此没有帮助。您可以使用array_filter和explode。
$chapters = array_filter(explode("\r\n", $lines), function ($line) {
$line = trim($line);
return substr($line, 0, 7) === 'Chapter';
});那么$chapters应该是这样的:
array(
"Chapter 1: New One",
"Chapter 2 Second One this is long",
"Chapter 3 new this is long"
);我的PHP有点生疏,但应该能让你更接近!
https://stackoverflow.com/questions/24633100
复制相似问题