这是我要搜索的文本值,此文本中没有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 Simple HTML DOM,但不知道如何使用different format
和length
提取这些章节。
答案 0 :(得分:2)
你的意思是这样的......?
<?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
答案 1 :(得分:1)
没有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有点生疏,但应该让你亲近!