保存.php文件并保存包含(可能) [英] Saving a .php file and saving the includes too (possibly)

查看:94
本文介绍了保存.php文件并保存包含(可能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我有一个标准的.php文件(index.php),其中包含两个包含,一个用于标题(header.php),另一个用于页脚(footer.php)。 index.php文件如下所示:

I have a standard .php file (index.php) that contains two includes, one for header (header.php) and one for footer (footer.php). The index.php file looks like this:

index.php

index.php

<?php
include header.php;
?>

<h2>Hello</h2>
<p class="editable">Lorem ipsum dolar doo dah day</p>

<?php
include footer.php;
?>

header.php如下:

header.php like this:

<html>
<head>
<title>This is my page</title>
</head>
<body>
<h1 class="editable">My Website rocks</h1>

和页脚.php如下:

<p>The end of my page</p>
</body>

我正在编写一个PHP脚本,允许您编辑任何.editable项目页。我的问题是这些可编辑区域可能出现在任何包含的文件以及index.php的主体中。

I am writing a PHP script that allows you to edit any of the ".editable" items on a page. My problem is that these editable regions could appear in any included files as well as the main body of index.php.

我的php代码正在使用file_get_contents()获取index.php文件;效果很好。我也可以在index.php中编辑和保存任何.editable区域。

My php code is grabbing the index.php file with file_get_contents(); which works well. I am also able to edit and save any ".editable" regions in index.php.

我的问题:

我一直无法找到查找包含和解析的方法那些.editable地区也是如此。我正在寻找有关如何处理index.php中所有包含的建议 - 检查它们是否可编辑区域。我是否需要使用正则表达式来查找include * .php?我不确定从哪里开始...

I have been unable to find a way of "finding" the includes and parse through those for ".editable" regions as well. I am looking for suggestions on how I would work through all the includes in index.php - checking them for editable regions. Would I need to use regular expressions to find "include *.php"? I am unsure of where to even start...

对于那些可能希望看到我的PHP代码的人。我正在使用PHP类:[link text] [1]它允许我编写如下代码:

For those of you who may wish to see my PHP code. I am making use of the PHP class: [link text][1] which allows me to write code like:

// load the class and file
$html = new simple_html_dom();
$html->load_file("index.php");

// find the first editable area and change its content to "edited"  
$html->find('*[class*=editable]', 0)->innertext = "Edited";

// save the file
$html->save(index.php);

[1]: http://simplehtmldom.sourceforge.net/manual_api.htm 简单的php dom解析器

UPDATE

我一直在玩正则表达式来尝试匹配包含。我对正则表达式很垃圾,但我认为我越来越近了。以下是我到目前为止:

I have been playing around with regular expressions to try and match the includes. I am pretty rubbish at regex but I think I am getting close. Here is what I have so far:

$findinclude = '/(?:include|include_once|require|require_once)\s*(?:[a-z]|"|\(|\)|\'|_|\.|\s|\/)*(?=(?:[^\<\?]|[^\?\>])*\?>)/i';

这相当匹配,虽然它似乎返回奇数)和'当使用preg_match时。我试图在正则表达式中添加一些安全性以确保它只在php标记之间匹配 - 这部分:( ?=(?:[^ \< \?] | [^ \?>])* \?>) - 但它只返回页面上的第一个包含。有关如何改进此常规的任何提示表达式?(我已经待了大约6个小时)

This matches fairly well although it does seem to return the odd ) and ' when using preg_match. I am trying to add a bit of security into the regex to ensure it only matches between php tags - this part: (?=(?:[^\<\?]|[^\?>])*\?>) - but it only returns the first include on a page. Any tips on how to improve this regular expression? (I have been at it for about 6 hours)

推荐答案

好的,我终于解决了。如果有人在看要在.php文件中找到任何include,include_once,require,require_once,那么你可以使用像preg_match_all这样的php函数使用以下正则表达式。

Ok, I finally worked it out. If anyone is looking to find any include, include_once, require, require_once in a .php file then you can use the following regular expression with a php function like preg_match_all.

'/(?:include|include_once|require|require_once)\s*(?:[a-z]|"|\(|\)|\'|_|\.|\s|\/)*(?=(?:[^\<\?])*\?>)/i';

这会在标签中查找任何包含等。将此引用回原始示例。我的代码如下所示:

This looks for any includes etc within tags. Referencing this back to my original example. My code looks like this:

$html = new simple_html_dom();
$html->load_file("index.php");

$findinclude = '/(?:include|include_once|require|require_once)\s*(?:[a-z]|"|\(|\)|\'|_|\.|\s|\/)*(?=(?:[^\<\?])*\?>)/i';

if (preg_match_all($findinclude, $html,$includes)):

    // shift the array to the left
    $incfiles = $includes[0];
    $i = 0;

    // then loop through the includes array and print our filename
    foreach ($incfiles as $inc) {
       print basename(preg_replace('/[^a-zA-Z0-9\s\.\_\/]/', '', $inc)."\n");
    }
endif;

完成工作!现在我可以根据需要编辑每个文件。

Job done! I can now work through this to edit each file as required.

这篇关于保存.php文件并保存包含(可能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆