PHP-内部HTML递归替换

PHP-内部HTML递归替换,php,regex,innerhtml,Php,Regex,Innerhtml,我需要对HTML的一部分执行递归str_替换(首先使用递归,我指的是内部节点),因此我写道: $str = //get HTML; $pttOpen = '(\w+) *([^<]{1,100}?)'; $pttClose = '\w+'; $pttHtml = '(?:(?!(?:<x-)).+)'; while (preg_match("%<x-(?:$pttOpen)>($pttHtml)*</x-($pttClose)>%m", $str, $mat

我需要对HTML的一部分执行递归str_替换(首先使用递归,我指的是内部节点),因此我写道:

$str = //get HTML;
$pttOpen = '(\w+) *([^<]{1,100}?)';
$pttClose = '\w+';
$pttHtml = '(?:(?!(?:<x-)).+)';

while (preg_match("%<x-(?:$pttOpen)>($pttHtml)*</x-($pttClose)>%m", $str, $match)) {
    list($outerHtml, $open, $attributes, $innerHtml, $close) = $match;
    $newHtml = //some work....
    str_replace($outerHtml, $newHtml, $str);
}
$str=//获取HTML;
$pttOpen='(\w+)*([^试试这个正则表达式:

%试试这个正则表达式:

%试试这个正则表达式:

%试试这个正则表达式:


%多亏了@Alex,我想到了这个:

%<x-(?P<open>\w+)\s*(?P<attributes>[^>]*?)>(?P<innerHtml>((?!<x-).)*)</x-(?P=open)>%is

%]*?)>(?P((?!

多亏了@Alex,我想出了这个:

%<x-(?P<open>\w+)\s*(?P<attributes>[^>]*?)>(?P<innerHtml>((?!<x-).)*)</x-(?P=open)>%is

%]*?)>(?P((?!

多亏了@Alex,我想出了这个:

%<x-(?P<open>\w+)\s*(?P<attributes>[^>]*?)>(?P<innerHtml>((?!<x-).)*)</x-(?P=open)>%is

%]*?)>(?P((?!

多亏了@Alex,我想出了这个:

%<x-(?P<open>\w+)\s*(?P<attributes>[^>]*?)>(?P<innerHtml>((?!<x-).)*)</x-(?P=open)>%is

%]*?)>(?P((?!

我不知道您正试图做什么样的更改,但这是我将继续进行的方式:

$pattern = <<<'EOD'
~
    <x-(?<tagName>\w++) (?<attributes>[^>]*+) >
    (?<content>(?>[^<]++|<(?!/?x-))*) #by far more efficient than (?:(?!</?x-).)*
    </x-\g<tagName>>
~x
EOD;

function callback($m) { // exemple function
    return '<n-' . $m['tagName'] . $m['attributes'] . '>' . $m['content']
         . '</n-' . $m['tagName'] . '>';       
};

do {
    $code = preg_replace_callback($pattern, 'callback', $code, -1, $count);
} while ($count);


echo htmlspecialchars(print_r($code, true));

$pattern=我不知道您到底想做什么样的更改,但我会这样做:

$pattern = <<<'EOD'
~
    <x-(?<tagName>\w++) (?<attributes>[^>]*+) >
    (?<content>(?>[^<]++|<(?!/?x-))*) #by far more efficient than (?:(?!</?x-).)*
    </x-\g<tagName>>
~x
EOD;

function callback($m) { // exemple function
    return '<n-' . $m['tagName'] . $m['attributes'] . '>' . $m['content']
         . '</n-' . $m['tagName'] . '>';       
};

do {
    $code = preg_replace_callback($pattern, 'callback', $code, -1, $count);
} while ($count);


echo htmlspecialchars(print_r($code, true));

$pattern=我不知道您到底想做什么样的更改,但我会这样做:

$pattern = <<<'EOD'
~
    <x-(?<tagName>\w++) (?<attributes>[^>]*+) >
    (?<content>(?>[^<]++|<(?!/?x-))*) #by far more efficient than (?:(?!</?x-).)*
    </x-\g<tagName>>
~x
EOD;

function callback($m) { // exemple function
    return '<n-' . $m['tagName'] . $m['attributes'] . '>' . $m['content']
         . '</n-' . $m['tagName'] . '>';       
};

do {
    $code = preg_replace_callback($pattern, 'callback', $code, -1, $count);
} while ($count);


echo htmlspecialchars(print_r($code, true));

$pattern=我不知道您到底想做什么样的更改,但我会这样做:

$pattern = <<<'EOD'
~
    <x-(?<tagName>\w++) (?<attributes>[^>]*+) >
    (?<content>(?>[^<]++|<(?!/?x-))*) #by far more efficient than (?:(?!</?x-).)*
    </x-\g<tagName>>
~x
EOD;

function callback($m) { // exemple function
    return '<n-' . $m['tagName'] . $m['attributes'] . '>' . $m['content']
         . '</n-' . $m['tagName'] . '>';       
};

do {
    $code = preg_replace_callback($pattern, 'callback', $code, -1, $count);
} while ($count);


echo htmlspecialchars(print_r($code, true));


$pattern=你能在你的问题中添加一个预期输出的例子吗?@casimirithippolyte几乎同时问同一个问题!:)修饰符m将锚定
^
$
(你不使用的)的含义更改为“行的开始”和“行的结束”您使用
$newHTML
制作的代码也很有用。对HTML代码的操作->始终使用DOM解析器,而不是正则表达式。(xpath、domdocument、simplexml、sax…)您能在问题中添加一个具有预期输出的示例吗?@CasimiretHippolyte几乎同时使用同一个问题!:)修饰符m将锚点
^
$
(您不使用)的含义更改为“行的开始”和“行的结束”。您使用
$newHTML
所做的“工作”也很有用。对HTML代码的操作->始终使用DOM解析器,而不是正则表达式(xpath、domdocument、simplexml、sax…)你能在你的问题中添加一个预期输出的例子吗?@casimimirithippolyte几乎同时出现同一个问题!:)修饰符m将锚定
^
$
(你不使用)的含义更改为“行的开始”和“行的结束”您使用
$newHTML
制作的代码也很有用。对HTML代码的操作->始终使用DOM解析器,而不是正则表达式。(xpath、domdocument、simplexml、sax…)您能在问题中添加一个具有预期输出的示例吗?@CasimiretHippolyte几乎同时使用同一个问题!:)修饰符m将锚点
^
$
(您不使用)的含义更改为“行的开始”和“行的结束”。您使用
$newHTML
所做的“工作”也很有用。对HTML代码的操作->始终使用DOM解析器,而不是正则表达式(xpath、domdocument、simplexml、sax…)@Casimirithippolyte我用你的话更新了我的答案。@Casimirithippolyte我用你的话更新了我的答案。@Casimirithippolyte我用你的话更新了我的答案。@Alex:是在Yves Saint Laurent和Wordpress之间。@Alex:当然!@Alex:是在Yves Sain之间洛朗和Wordpress。@Alex:当然!@Alex:在伊夫·圣洛朗和Wordpress之间。@Alex:当然!@Alex:在伊夫·圣洛朗和Wordpress之间。@Alex:当然!