2014-07-08 74 views
0

这里得到具体文本的文本值我试图搜索,本文没有蚂蚁HTML它(这就是个问题HTML DOM不工作)PHP从纯文本

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 OneChapter 2 Second One this is long种类的值,还有更多的章节可以获得。

我正在尝试PHP简单的HTML DOM,但不知道如何从different formatlength中提取这些章节。

回答

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

有没有DOM因此使用方法,不会帮助。您可以使用array_filterexplode

$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是一种生疏,但应该让你靠近!