Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从纯文本中获取特定文本_Php_Html_Dom - Fatal编程技术网

PHP从纯文本中获取特定文本

PHP从纯文本中获取特定文本,php,html,dom,Php,Html,Dom,这是我试图搜索的文本值,该文本中没有ant HTML(这是HTML DOM不起作用的问题) 好的,现在我正在尝试获取第1章:新的和第2章第二个这是很长的类值,还有更多的章节要获取 我在尝试PHP简单HTML DOM,但不知道如何使用不同格式和长度来提取这些章节,你是说这样的吗 <?php $lines = " User Guide For iOS 7.1 Software Contents Chapter 1: New One iPhone at

这是我试图搜索的文本值,该文本中没有ant HTML(这是HTML DOM不起作用的问题)

好的,现在我正在尝试获取
第1章:新的
第2章第二个这是很长的
类值,还有更多的章节要获取


我在尝试PHP简单HTML DOM,但不知道如何使用
不同格式和
长度来提取这些章节,你是说这样的吗

<?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>";
        }
    }
}

你是说像这样的事吗

<?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>";
        }
    }
}

你是说像这样的事吗

<?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>";
        }
    }
}

你是说像这样的事吗

<?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>";
        }
    }
}

因为没有DOM,所以对它使用方法是没有帮助的。您可以使用
array\u 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有点生锈了,但应该可以让你接近

没有DOM,因此使用方法来实现它不会有帮助。您可以使用
array\u 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有点生锈了,但应该可以让你接近

没有DOM,因此使用方法来实现它不会有帮助。您可以使用
array\u 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有点生锈了,但应该可以让你接近

没有DOM,因此使用方法来实现它不会有帮助。您可以使用
array\u 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有点生锈了,但应该可以让你接近