简体   繁体   English

带有PHP逻辑的标记之间的PHP preg_match

[英]PHP preg_match between tags with PHP logic inside

I have a dynamic sitemap creation script that recursively looks through the filesystem and when needed, opens the start of a file, then looks for the text between tags. 我有一个动态站点地图创建脚本,该脚本递归地浏览文件系统,并在需要时打开文件的开头,然后在标记之间查找文本。 It worked when the setup was like... 它在安装时就像...

<title>Page Title | Company, Inc.</title>

But now I've added a wrinkle and set up a way to manage titles and meta info via an admin tool. 但是,现在我增加了一些麻烦,并设置了一种通过管理工具管理标题和元信息的方法。 Just in case something isn't entered, I want to fall back to the old default title info, but my preg_match isn't working for this... 以防万一没有输入内容,我想回到原来的默认标题信息,但是我的preg_match无法正常工作...

<title><?=@$page_meta_array['page_title']!=''?$page_meta_array['page_title']:"Default Title Here";?> | Company, Inc.</title>

The php function that I pass the page to looks like this... 我将页面传递给的php函数如下所示...

function get_title($filename) {
    $retval = "";
    $handle = fopen($filename, "r");
    $head = fread($handle, 4096);
    preg_match(";<title>(.+)</title>;", $head, $matches);
    if(sizeof($matches) == 2) {
        $retval = trim($matches[1]);
    }
    fclose($handle);

    return $retval;
}

Can someone point me to the correct preg_match? 有人可以指出我正确的preg_match吗? I'd like to have "Default Title Here" returned from the second example above. 我想从上面的第二个示例中返回“ Default Title Here”。

Thanks 谢谢

this might do it: 这可能会做到:

preg_match(';<title><\?[^>]*"([^"]*)"[^>]*></title>;', $head, $matches);

note that short-tags are deprecated, and may not work in PHP6. 请注意,不赞成使用短标记,并且在PHP6中可能无法使用。

you first need to parse the PHP code, then check for matches. 您首先需要解析PHP代码,然后检查匹配项。 Why don't you just use the contents of $page_meta_array['page_title']? 您为什么不只使用$ page_meta_array ['page_title']的内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM