从 PHP 中的字符串中提取文本
Extract Text from a string in PHP
我有一个字符串,其中包含:
navigation pane or double clicking folders in the details pane.
Navigating the folder structure
-------------------------------
symbol will expand or collapse folders in the navigation pane
我只想提取文本"Navigating the folder structure"
我试过 preg_match_all 但无法处理换行的起点。
我也尝试过使用 strpos 但无法以新行作为起点。
使用 strpos
的组合来查找换行符,使用 substr
来提取第一和第二个换行符之间的文本。此文本包含破折号 (-
),因此我包含了一个 str_replace
来删除它们。
$string = 'navigation pane or double clicking folders in the details pane.
Navigating the folder structure
-------------------------------
symbol will expand or collapse folders in the navigation pane';
$position1 = strpos($string, "\n");
$position2 = strpos($string, "\n",$position1);
$extracted = str_replace("-","",substr($string, $position1, $position2));
echo $extracted;
这将打印所需的文本
使用explode
-
$new = explode("\n", $str);
var_dump($new[2]);
我有一个字符串,其中包含:
navigation pane or double clicking folders in the details pane.
Navigating the folder structure
-------------------------------
symbol will expand or collapse folders in the navigation pane
我只想提取文本"Navigating the folder structure"
我试过 preg_match_all 但无法处理换行的起点。
我也尝试过使用 strpos 但无法以新行作为起点。
使用 strpos
的组合来查找换行符,使用 substr
来提取第一和第二个换行符之间的文本。此文本包含破折号 (-
),因此我包含了一个 str_replace
来删除它们。
$string = 'navigation pane or double clicking folders in the details pane.
Navigating the folder structure
-------------------------------
symbol will expand or collapse folders in the navigation pane';
$position1 = strpos($string, "\n");
$position2 = strpos($string, "\n",$position1);
$extracted = str_replace("-","",substr($string, $position1, $position2));
echo $extracted;
这将打印所需的文本
使用explode
-
$new = explode("\n", $str);
var_dump($new[2]);