Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP替换XHTML标记并通过preg_replace添加参数_Php_Regex_Parsing - Fatal编程技术网

使用PHP替换XHTML标记并通过preg_replace添加参数

使用PHP替换XHTML标记并通过preg_replace添加参数,php,regex,parsing,Php,Regex,Parsing,假设您有以下代码(由具有唯一ID的指定的部分是多个): 目前,我正在努力使用正则表达式来检查换行符,并限制它单独修改每篇文章。它所做的是将第一个和最后一个替换为一篇文章的内容。我使用的代码如下(所有内容都存储在变量$text中;s修饰符包括换行符): $text=preg\u replace(“\(.+)\\(.+?)\\\\\\\\\\\\\\\\”,“$3',$text”); $text=preg\u replace(“\35;\这里有一个小小的起点。这可能不是最简单的方法,也不完全符合您的

假设您有以下代码(由具有唯一ID的
指定的部分是多个):

目前,我正在努力使用正则表达式来检查换行符,并限制它单独修改每篇文章。它所做的是将第一个
和最后一个
替换为一篇文章的内容。我使用的代码如下(所有内容都存储在变量
$text
中;
s
修饰符包括换行符):

$text=preg\u replace(“\(.+)\

\(.+?)\\\\\\\\\\\\\\\\”,“

$3',$text”);


$text=preg\u replace(“\35;\这里有一个小小的起点。这可能不是最简单的方法,也不完全符合您的规范,但我希望它能给您一些开始编码的想法:

<?php

$fragment = '<art id="001">
<p class="prim"><h1>word1</h1>Text</p>
</art>

<art id="002">
<p class="prim"><h1>word2</h1>Text</p>
<p class="sec"><h1>word2.1</h1>Text</p>
</art>';

$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTML($fragment);
libxml_use_internal_errors(FALSE);

$articles = $dom->getElementsByTagName('art');
foreach($articles as $article){
    $titles = $article->getElementsByTagName('h1');
    if($titles->length>0){
        $title = $titles->item(0)->nodeValue;

        $index = $dom->createElement('index');
        $index->setAttribute('value', $title);

        $article->appendChild($index);
    }
}

$dom->formatOutput = TRUE;
$dom->preserveWhiteSpace = TRUE;
echo $dom->saveHTML();
loadHTML($fragment);
libxml\u使用\u内部错误(FALSE);
$articles=$dom->getElementsByTagName('art');
foreach($articles作为$article){
$titles=$article->getElementsByTagName('h1');
如果($titles->length>0){
$title=$titles->item(0)->nodeValue;
$index=$dom->createElement('index');
$index->setAttribute('value',$title);
$article->appendChild($index);
}
}
$dom->formatOutput=TRUE;
$dom->preserveWhiteSpace=TRUE;
echo$dom->saveHTML();
…将打印以下内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<art id="001"><p class="prim"></p>
<h1>word1</h1>Text
<index value="word1"></index></art><art id="002"><p class="prim"></p>
<h1>word2</h1>Text
<p class="sec"></p>
<h1>word2.1</h1>Text
<index value="word2"></index></art>
</body></html>

文字1文本

word2Text

文字2.1文字
是否需要使用regexp来完成?使用任何内置DOM库,这类任务都会变得更容易。@lvaroG.Vicario:当然。我以前从未做过这样的任务,所以我选择了我发现的第一种方法。您可能想阅读-因为在编程中,一般来说,一切都是有争议的,您可能还想ead另一个观点:。如果我是你,我会选择OK。不管怎样,我已经发布了一个非regexp答案。希望它能有所帮助。谢谢你,这是一个有趣的阅读材料。非常感谢。这确实很有帮助,因为我对这个DOM的知识是空的,所以演示很方便。还有一件事:你能告诉我如何支持utf-8吗?例如,返回拉丁字符,例如作为ÅÄľÅÅÃýÄ。这是一个广泛的问题。请尝试从中的第二个参数开始。
$text = preg_replace("#\<art id=\"(.+)\"\>(.+)\<p class=\"prim\"\>\<h1\>(.+?)\</h1\>#s", '<tra id="$1" title="$3"><index value="$3" /><p class="main"><h2>$3</h2>', $text);
$text = preg_replace("#\</art#", '</tra', $text);
<?php

$fragment = '<art id="001">
<p class="prim"><h1>word1</h1>Text</p>
</art>

<art id="002">
<p class="prim"><h1>word2</h1>Text</p>
<p class="sec"><h1>word2.1</h1>Text</p>
</art>';

$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTML($fragment);
libxml_use_internal_errors(FALSE);

$articles = $dom->getElementsByTagName('art');
foreach($articles as $article){
    $titles = $article->getElementsByTagName('h1');
    if($titles->length>0){
        $title = $titles->item(0)->nodeValue;

        $index = $dom->createElement('index');
        $index->setAttribute('value', $title);

        $article->appendChild($index);
    }
}

$dom->formatOutput = TRUE;
$dom->preserveWhiteSpace = TRUE;
echo $dom->saveHTML();
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<art id="001"><p class="prim"></p>
<h1>word1</h1>Text
<index value="word1"></index></art><art id="002"><p class="prim"></p>
<h1>word2</h1>Text
<p class="sec"></p>
<h1>word2.1</h1>Text
<index value="word2"></index></art>
</body></html>