file_get_contents使用Get字符串提取正文



如何提取这个php与get字符串?

<Body>
"Example"
</body>

那么我只需要"Example">

文件获取内容的输出示例->获取字符串函数

您可以使用正则表达式来匹配<body></body>,并捕获其中的文本:

<?php

// Using HEREDOC synatx for illustration
// $str = file_get_contents($file); should work as well
$str = <<<EOT
start<body>
Some text
Some more text
</body>
text after body
EOT;
$regex = "/.*<body>(.*?)</body>/ms";
$result = preg_match( $regex, $str, $matches);
print_r($matches[1]);

见https://3v4l.org/dl2hr

注意这是从HTML中提取数据的正则表达式的一种非常有限的使用。您不应该将此作为广泛使用的基本示例。为此,您应该使用DOMDocument

最新更新