php intercepts the string and retains the full function code of the xml tag

  • 2020-05-30 19:41:49
  • OfStack


<?php  
    /** 
     * author: goosman 
     * blog: http://blog.csdn.net/lgg201 
     * mail: lgg860911@yahoo.com.cn 
     */  

    $str    = '0123456789<a>012</a>0123456789';  
    function substr_remain_tag($s, $o, $l) {  
        $is_match   = preg_match_all(<<<heredoc  

    # The regular expression is parsed xml The label ,  Escape characters are supported inside tag properties "\",  Support for "\" The escape of itself and the corresponding quotation marks   
    <(\w+)             # Tags start   
        (?:          # Property list   
            \s+       # Leading Spaces   
            \w+    # The property name  
            \s*    # White space after the property name ( In order to compatible )  
            =        # Equal sign between property name values   
            \s*       # White space before property values ( In order to compatible )  
            (?:         # Attribute values ( Quotes processing )  
                "         # Double quotation marks   
                (?:  
                    \\\\\\\\   # Eat two escape characters in a row ( Represents the escape character itself )  

                    \\\\"          # Eat the escape character next 1 The quotation ( Quotation marks for escape )  

                    [^"\\\\]*   # Other characters   
                )*  
                "  

                '       # Single quotation mark case   
                (?:  
                    \\\\\\\\   # Eat two escape characters in a row ( Represents the escape character itself )  

                    \\\\'   # Eat the escape character next 1 The quotation ( Quotation marks for escape )  

                    [^'\\\\]*       # Other characters   
                )*  
                '  
            )  
        )*  
    >  
    .*?               # Label content   
    </(?1)>     # End tag   
    ;x  
    heredoc  
    , $s, $matches, PREG_OFFSET_CAPTURE, $o);  
        if ( $is_match ) {  
            foreach ( $matches[0] as $match ) {  
                $o0 = $match[1];  
                # The left edge of the tag crosses the right edge of the intercept target ,  exit   
                if ( $o0 >= $o + $l ) break;  
                $l0 = strlen($match[0]);  
                # The right border of the tag is inside the right border of the interception target ,  Continue to   
                if ( $o0 + $l0 < $o + $l ) continue;  

                # The following is the label transboundary processing   
                $l  = $o0 + $l0 - $o;  
                break;  
            }  
        }  
        return substr($s, $o, $l);  
    }   
    echo $str . chr(10);  
    echo substr_remain_tag($str, 0, 20) . chr(10); 

Related articles: