2014-07-08 72 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是一種生疏,但應該讓你靠近!