PHP从纯文本中获取特定文本
这是我试图搜索的文本值,该文本中没有ant HTML(这是HTML DOM不起作用的问题) 好的,现在我正在尝试获取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
第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有点生锈了,但应该可以让你接近