Php 更改标记关闭的位置

Php 更改标记关闭的位置,php,regex,Php,Regex,我有一个包含html内容的字符串,其中的链接关闭在错误的位置,我需要将其放置在正确的位置。让我解释一下模式: 所有链接后面都有一个标记或一个*。我需要在*或之后关闭a标记。例如: <a href="#" class="ddb1"><sup id="v3534" class="ddb17">34</sup> No harás impura la tierra en que habitáis, porque yo habito en medio de ella,

我有一个包含html内容的字符串,其中的链接关闭在错误的位置,我需要将其放置在正确的位置。让我解释一下模式:

所有链接后面都有一个
标记或一个
*
。我需要在
*
之后关闭
a
标记。例如:

<a href="#" class="ddb1"><sup id="v3534" class="ddb17">34</sup> No harás impura la tierra en que habitáis, porque yo habito en medio de ella, pues yo, Yahvé, tengo mi morada entre los israelitas.»</a>
注意:

该字符串具有类似于
的拉丁字符

编辑:

我的这段代码适用于前两个示例,但不适用于第三个示例(上面说的是真实字符串的示例):

函数sMove($string){
preg_匹配('/(.**?)/si',$string,$atags);
if(isset($atags[0])){
if(preg_match(“/(.*?)/si',$atags[0],$matches)){
$sup=$matches[0];
$text=str_replace($sup,,$atags[1]);
$string=str_replace($text,,$string)。$text;
}
if(预匹配('/\*./',$atags[0],$MATCHS)){
$string=str_replace($matches[0],“*”,$string)。”;
}
}
返回$string;
}
您可以(不是说您应该)使用以下表达式:

<a[^>]*>(?:\*|<sup[^>]*>.*?</sup>)\K(.*?)</a>
(注意修饰符!)。

分解如下:

<a[^>]*>                   # likely an opening anchor tag
(?:\*|<sup[^>]*>.*?</sup>) # * or <sup...>...</sup>
\K                         # forget what's been matched thus far
                           # (but remember the position)
(.*?)                      # match anything lazily...
</a>                       # ... up to </a>
]*>#很可能是一个开始的锚标签
(?:\*.]*>.*)或。。。
\K#忘了到目前为止匹配的是什么
#(但记住位置)
(.*)#懒洋洋地匹配任何东西。。。
# ... 高达

但是,考虑使用解析器和<代码> DOM函数,因为这是一个有点杂乱和错误的嵌套标签和属性(即<代码>数据标签< /代码>).< /p>可以缩短,取出1k宽字符串中的垃圾文本吗?

La herencia de la mujer casada<a href="notas04nm.xhtml#nnm170ref" class="ddb1">*</a>.
<sup id="v3530" class="ddb17">30</sup> «En cualquier caso de homicidio, se matará al homicida según la declaración de los testigos; pero un solo testigo no bastará para condenar a muerte a un hombre*. <sup id="v3531" class="ddb17">31</sup> No aceptaréis rescate por la vida de un homicida reo de muerte, pues debe morir. <sup id="v3532" class="ddb17">32</sup> Tampoco aceptaréis rescate por el que se ha refugiado en la ciudad de asilo y quiere volver a habitar en su tierra antes que muera el Sumo Sacerdote. <a href="#" class="ddb1"><sup id="v3533" class="ddb17">33</sup> No profanaréis la tierra en que estáis, porque aquella sangre profana la tierra, y la tierra no queda expiada de la sangre derramada más que con la sangre del que la derramó. </a><a href="#" class="ddb1"><sup id="v3534" class="ddb17">34</sup> No harás impura la tierra en que habitáis, porque yo habito en medio de ella, pues yo, Yahvé, tengo mi morada entre los israelitas.»</a> La herencia de la mujer casada<a href="notas04nm.xhtml#nnm170ref" class="ddb1">*.</a>
function sMove($string){
    preg_match('/<a.*?\>(.*?)<\/a>/si', $string, $atags);
    if ( isset( $atags[0] )) {
        if (preg_match('/<sup.*?\>(.*?)<\/sup>/si', $atags[0], $matches)) {
            $sup = $matches[0];
            $text = str_replace($sup, '', $atags[1]);
            $string = str_replace($text, '', $string) . $text;       
        }
        if (preg_match('/\*./', $atags[0], $matches)) {
            $string = str_replace($matches[0], '*', $string) . '.';
        }
    }
    return $string;
}
<a[^>]*>(?:\*|<sup[^>]*>.*?</sup>)\K(.*?)</a>
</a>$1
<a[^>]*>                   # likely an opening anchor tag
(?:\*|<sup[^>]*>.*?</sup>) # * or <sup...>...</sup>
\K                         # forget what's been matched thus far
                           # (but remember the position)
(.*?)                      # match anything lazily...
</a>                       # ... up to </a>