PHP能否只包含文件中指定部分的工作?

PHP能否只包含文件中指定部分的工作?,php,include,Php,Include,我有一个PHP include,它将一个html表单插入到另一个html表单中。当表单包含进来时,我现在有两个表单头。是否有一个php标签,我可以使用它来让我 <form id="orderform"> <!-- <?php for the include FROM here?> --> PROD:<input class="ProductName" type="text" size=&qu

我有一个PHP include,它将一个html表单插入到另一个html表单中。当表单包含进来时,我现在有两个表单头。是否有一个php标签,我可以使用它来让我

<form id="orderform">

<!-- <?php for the include FROM here?> -->

PROD:<input class="ProductName" type="text" size="75">|
Discount:<input class="Discount" type="text" size="3">|
QTY:<input class="qty" type="text" size="6">|
Line/Total:<input class="LineTotal" type="text" size="9" disabled>

<!-- <?php for the include TO here?> -->

</form>

产品:|
折扣:|
数量:|
行/总数:
那么include将进入该文件并获得指定的HTML

这可能吗

编辑


这只返回文本。如何在divtagid中获取HTML表单元素?

为此,最好查看php函数

<?php
function form_tag_wrapper($form) {
  return '<form id="orderform">'. $form .'</form>';
}

function form_contents() {
  return 'PROD:<input class="ProductName" type="text" size="75">
          ...
          Line/Total:<input class="LineTotal" type="text" size="9" disabled>';
}
?>
或者,作为一个单行线:

print form_tag_wrapper(form_contents());

为此,您最好研究一下php函数

<?php
function form_tag_wrapper($form) {
  return '<form id="orderform">'. $form .'</form>';
}

function form_contents() {
  return 'PROD:<input class="ProductName" type="text" size="75">
          ...
          Line/Total:<input class="LineTotal" type="text" size="9" disabled>';
}
?>
或者,作为一个单行线:

print form_tag_wrapper(form_contents());

可以使用()将include文件构造为XML文件。这样的文件只会丢失XML根元素,可以动态添加

这样做的好处是,只要使用标准标记(如
)编写PHP,它就可以与XHTML和PHP兼容,因为这些标记是有效的XML处理指令。让我们从一个简单的、仅使用XHTML的示例开始,PHP代码的原理实际上是相同的,但为什么要使其复杂化呢

include.php:


正文:
要创建能够处理此问题的include函数,它只需要解析XML文件并返回有问题的片段,例如
标记中的所有内容。XML解析随PHP一起提供,因此一切都已准备就绪

function incXML($file, $id = NULL)
{
    if (!($xml = file_get_contents($file)))
        throw new Exception('File IO error.');

    $xml = '<template>' . $xml . '</template>';

    $doc = new DOMDocument();
    if (!$doc->loadXML($xml))
        throw new InvalidArgumentException('Invalid file.');

    // @todo add HTML namespace if you want to support it

    // fetch the contents of the file however you need to,
    // e.g. with an xpath query, I leave this for a training
    // and just do a plastic getElementById here which does NOT
    // work w/o proper DTD support, it's just for illustration.

    if (NULL === $id)
    {
        $part = $doc;
    }
    else
    {
        if (!$part = $doc->getElementById($id))
            throw new InvalidArgumentException('Invalid ID.');
    }

    $buffer = '';
    foreach($part->childNodes as $node)
    {
        $buffer .= $doc->saveXML($node);
    }

    return 'data:text/html;base64,' . base64_encode($buffer);
}
函数incXML($file,$id=NULL) { 如果(!($xml=file\u获取内容($file))) 抛出新异常(“文件IO错误”); $xml='.$xml''; $doc=新的DOMDocument(); 如果(!$doc->loadXML($xml)) 抛出新的InvalidArgumentException(“无效文件”); //@todo添加HTML命名空间(如果您想支持它) //以您需要的方式获取文件的内容, //例如,对于xpath查询,我将其留给培训 //在这里做一个塑料getElementById,它不会 //在没有适当DTD支持的情况下工作,仅用于说明。 如果(NULL==$id) { $part=$doc; } 其他的 { 如果(!$part=$doc->getElementById($id)) 抛出新的InvalidArgumentException('Invalid ID'); } $buffer=''; foreach($part->childNodes作为$node) { $buffer.=$doc->saveXML($node); } 返回'data:text/html;base64',.base64_encode($buffer); } 用法:

<?php
    include(incXML('include.php', 'orderform'))
?>


使用这种方法,您不需要更改任何模板文件,只要它们最终包含XHTML/PHP。

您可以使用()将include文件构造为XML文件。这样的文件只会丢失XML根元素,可以动态添加

这样做的好处是,只要使用标准标记(如
)编写PHP,它就可以与XHTML和PHP兼容,因为这些标记是有效的XML处理指令。让我们从一个简单的、仅使用XHTML的示例开始,PHP代码的原理实际上是相同的,但为什么要使其复杂化呢

include.php:


正文:
要创建能够处理此问题的include函数,它只需要解析XML文件并返回有问题的片段,例如
标记中的所有内容。XML解析随PHP一起提供,因此一切都已准备就绪

function incXML($file, $id = NULL)
{
    if (!($xml = file_get_contents($file)))
        throw new Exception('File IO error.');

    $xml = '<template>' . $xml . '</template>';

    $doc = new DOMDocument();
    if (!$doc->loadXML($xml))
        throw new InvalidArgumentException('Invalid file.');

    // @todo add HTML namespace if you want to support it

    // fetch the contents of the file however you need to,
    // e.g. with an xpath query, I leave this for a training
    // and just do a plastic getElementById here which does NOT
    // work w/o proper DTD support, it's just for illustration.

    if (NULL === $id)
    {
        $part = $doc;
    }
    else
    {
        if (!$part = $doc->getElementById($id))
            throw new InvalidArgumentException('Invalid ID.');
    }

    $buffer = '';
    foreach($part->childNodes as $node)
    {
        $buffer .= $doc->saveXML($node);
    }

    return 'data:text/html;base64,' . base64_encode($buffer);
}
函数incXML($file,$id=NULL) { 如果(!($xml=file\u获取内容($file))) 抛出新异常(“文件IO错误”); $xml='.$xml''; $doc=新的DOMDocument(); 如果(!$doc->loadXML($xml)) 抛出新的InvalidArgumentException(“无效文件”); //@todo添加HTML命名空间(如果您想支持它) //以您需要的方式获取文件的内容, //例如,对于xpath查询,我将其留给培训 //在这里做一个塑料getElementById,它不会 //在没有适当DTD支持的情况下工作,仅用于说明。 如果(NULL==$id) { $part=$doc; } 其他的 { 如果(!$part=$doc->getElementById($id)) 抛出新的InvalidArgumentException('Invalid ID'); } $buffer=''; foreach($part->childNodes作为$node) { $buffer.=$doc->saveXML($node); } 返回'data:text/html;base64',.base64_encode($buffer); } 用法:

<?php
    include(incXML('include.php', 'orderform'))
?>


您不需要更改任何模板文件,只要它们最终使用这种方法包含XHTML/PHP。

不,没有其他方法可以包含文件的一部分。思考:您如何描述这样一个函数,从何处开始和结束包含?函数如何知道您想要什么

正如所建议的,最好的方法是重构包含的文件。如果这不是一个选项(我无法想象为什么不是),另一个途径是在包含的文件中使用变量或常量来表示应该输出哪些部分。大概是这样的:

#File: form_template.php
<?php if (!isset($include_form_header) || $include_form_header == true) { ?>
<form id="orderform">
<?php } ?>

PROD:<input class="ProductName" type="text" size="75">|
Discount:<input class="Discount" type="text" size="3">|
QTY:<input class="qty" type="text" size="6">|
Line/Total:<input class="LineTotal" type="text" size="9" disabled>

<?php if (!isset($include_form_header) || $include_form_header == true) { ?>
</form>
<?php } ?>
在我看来,这是讨厌的。当其他人(或未来的你)编辑
表单模板.php
时,设置
$include\u表单标题的时间或原因可能并不明显。这种对外部文件中声明的变量的依赖可能会导致出现意大利面代码

您最好为不同的目的构建单独的模板(或直接回显琐碎的输出,如打开或关闭表单的一行html),例如:

<?php
    #File: order_form_wrapper.php
    echo '<form id="orderform">';
?>

<?php
    #File: product_fields.php
    echo 'PROD:<input class="ProductName" type="text" size="75">';
    echo 'Discount:<input class="Discount" type="text" size="3">';
    echo 'QTY:<input class="qty" type="text" size="6">|';
    echo 'Line/Total:<input class="LineTotal" type="text" size="9" disabled>';
?>

// form with wrapper
include 'order_form_wrapper.php';
include 'product_fields.php';
echo '</form>';

// different form with wrapper
include 'some_other_form_wrapper.php';
include 'product_fields.php';
include 'some_other_fields.php';
echo '</form>';

//带包装的表格
包括“order_form_wrapper.php”;
包括“product_fields.php”;
回声';
//带包装的不同形式
包括“some_other_form_wrapper.php”;
包括“product_fields.php”;
包括“some_other_fields.php”;
回声';
最后一个选项是,如果您完全无法访问模板,无法修改模板,只能包含模板,那么您可以使用输出缓冲来包含文件,将生成的HTML加载到DOMDocument中,然后去掉包装表单标记。看看代码。。。这也不是很“整洁”:

函数提取形式公司
function extract_form_contents($template)
    // enable output buffer, include file, get buffer contents & turn off buffer
    ob_start();
    include $template;
    $form = ob_get_clean();
    ob_end_clean();

    // create a new DOMDocument & load the form HTML 
    $doc = new DOMDocument();
    $doc->loadHTML($form);

    // since we are working with an HTML fragment here, remove <!DOCTYPE, likewise remove <html><body></body></html> 
    $doc->removeChild($doc->firstChild);            
    $doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);

    // make a container for the extracted elements
    $div = $doc->createElement('div');

    // grab the form    
    $form = $doc->getElementsByTagName('form');
    if ($form->length < 1)
        return '';
    $form = $form->item(0);

    // loop the elements, clone, place in div
    $nb = $form->childNodes->length;
    if ($nb == 0)
        return '';
    for($pos=0; $pos<$nb; $pos++) {
        $node = $form->childNodes->item($pos);
        $div->appendChild($node->cloneNode(true));
    }
    // swap form for div
    $doc->replaceChild($div, $form);
    return $doc->saveHTML();
}
$contents = extract_form_contents('my_form_template.php');