使用stripos和substr查找文件的一部分

时间:2014-10-04 17:28:59

标签: php function substr stripos

我正在尝试找到文件中标题标记之间的部分。我知道文件正常工作且位置正确,但其余功能似乎无法正常工作。

function get_build_header() {
    $file = ('header.php');

    $startsAt = stripos($file, '<header>') + strlen('<header>');
    $endsAt = stripos($file, '</header>');
    $result = substr($file, $startsAt, $endsAt - $startsAt);
    require $result;
}

我一直在尝试返回,要求,绝望的任何事情让这个工作,以及file_get_contents但似乎没有任何改变。什么是阻止这种工作,因为$ starts还是$ endsAt似乎都没有返回任何东西?

1 个答案:

答案 0 :(得分:0)

使用file_get_contents()和正则表达式:preg_match('#<header>(.*)</header>#mis', $fileContents, $matches)您可以在$ matches [1]中找到标头标记的内容。