Home Backend Development PHP Tutorial php截取字符串并保留完整xml标签的函数代码_PHP

php截取字符串并保留完整xml标签的函数代码_PHP

Jun 01, 2016 pm 12:08 PM
xml tag Intercept string

复制代码 代码如下:
    /**
     * author: goosman
     * blog: http://blog.csdn.net/lgg201
     * mail: lgg860911@yahoo.com.cn
     */ 

    $str    = '01234567890120123456789'; 
    function substr_remain_tag($s, $o, $l) { 
        $is_match   = preg_match_all(
    #该正则表达式解析xml标签, 标签属性内部支持转义符"\", 支持对"\"自身和对应引号的转义 
            (?:          #属性列表 
            \s+       #前置空格 
            \w+    #属性名
            \s*    #属性名后的空白(为了兼容) 
            =        #属性名值之间的等号 
            \s*       #属性值前的空白(为了兼容) 
            (?:         #属性值(引号处理) 
                "         #双引号的情况 
                (?: 
                    \\\\\\\\   #吃掉连续两个转义符(表示转义符自身) 

                    \\\\"          #吃掉转义符接着一个引号(表示转义的引号) 

                    [^"\\\\]*   #其他字符 
                )* 
                " 

                '       #单引号情况 
                (?: 
                    \\\\\\\\   #吃掉连续两个转义符(表示转义符自身) 

                    \\\\'   #吃掉转义符接着一个引号(表示转义的引号) 

                    [^'\\\\]*       #其他字符 
                )* 
                ' 
            ) 
        )* 
    > 
    .*?               #标签内容 
    (?1)>     #结束标签 
    ;x 
    heredoc 
    , $s, $matches, PREG_OFFSET_CAPTURE, $o); 
        if ( $is_match ) { 
            foreach ( $matches[0] as $match ) { 
                $o0 = $match[1]; 
                #标签左边界越过截取目标右边界, 退出 
                if ( $o0 >= $o + $l ) break; 
                $l0 = strlen($match[0]); 
                #标签右边界在截取目标右边界内, 继续 
                if ( $o0 + $l0
                #以下为标签跨边界处理 
                $l  = $o0 + $l0 - $o; 
                break; 
            } 
        } 
        return substr($s, $o, $l); 
    }  
    echo $str . chr(10); 
    echo substr_remain_tag($str, 0, 20) . chr(10);
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to intercept a string in Go language How to intercept a string in Go language Mar 13, 2024 am 08:33 AM

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

How to intercept a string in go language How to intercept a string in go language Jan 12, 2023 pm 04:02 PM

Interception method: 1. Intercept a single character, the syntax is "string[index]", where "string" represents the source string, and "index" represents the character subscript to be obtained; 2. Intercept a substring, the syntax is "string[start: end" ]", where "start" represents the index of the first character to be intercepted (including this character when intercepting), "end" represents the index of the last character to be intercepted (excluding this character); 3. Get the entire String, syntax "string[:]".

How to use the LEFT function in MySQL to intercept the left part of a string How to use the LEFT function in MySQL to intercept the left part of a string Jul 12, 2023 pm 01:37 PM

How to use the LEFT function in MySQL to intercept the left part of a string. In database management systems, we often encounter situations where we need to intercept a certain part from a string. MySQL provides many built-in string functions, including the LEFT function, which can be used to intercept the left part of a string. The syntax of the LEFT function is as follows: LEFT (str, length) where str is the string to be intercepted and length is the length to be intercepted. Next, we will use code examples to demonstrate how

substr() function in PHP: how to intercept part of a string substr() function in PHP: how to intercept part of a string Nov 03, 2023 am 10:43 AM

The substr() function in PHP: How to intercept part of a string requires specific code examples. In PHP programming, string processing is one of the most common operations. Intercepting part of a string is a requirement that is often encountered when processing strings. In PHP, we can use the built-in substr() function to intercept part of a string. This article will introduce the usage of substr() function in detail and give specific code examples. The basic usage of the substr() function is as follows: string

How to use the RIGHT function in MySQL to intercept the right part of a string How to use the RIGHT function in MySQL to intercept the right part of a string Jul 12, 2023 am 10:20 AM

How to use the RIGHT function in MySQL to intercept the right part of a string. In MySQL, the RIGHT function is a function used to intercept the right part of a string. It accepts two parameters: the string to be intercepted and the length to be intercepted, and returns a string containing the specified length. Use the RIGHT function to get the right part of a string very conveniently. Below we will demonstrate how to use the RIGHT function through code examples. First, we need to create a sample data table to store the strings to be intercepted. CR

Use MySQL's LEFT function to intercept the specified length of the string Use MySQL's LEFT function to intercept the specified length of the string Jul 25, 2023 pm 05:04 PM

Use MySQL's LEFT function to intercept the specified length of a string. In MySQL, we often need to intercept strings to meet specific needs. Among them, the LEFT function is a very practical function that can intercept the specified length of a string. This article will introduce how to use MySQL's LEFT function to intercept strings and give code examples. First, we need to understand the syntax of the LEFT function. The basic syntax of the LEFT function is as follows: LEFT(string,lengt

How to intercept string in js How to intercept string in js Sep 21, 2023 am 10:45 AM

js uses the substring() method, the slice() method, the substr() method and the split() method to intercept strings. Detailed introduction: 1. The substring() method accepts two parameters, namely the starting position and the end position; 2. The slice() method allows the use of negative numbers as parameters, indicating that the index is calculated from the end of the string; 3. substr( ) method, which accepts two parameters, namely the starting position and the number of characters to be intercepted; 4. split() method, etc.

How to use regular expressions to verify attribute values ​​of XML tags in golang How to use regular expressions to verify attribute values ​​of XML tags in golang Jun 24, 2023 am 10:37 AM

Golang is a powerful programming language that can be used to write various types of applications. Among them, using regular expressions to verify attribute values ​​of XML tags is a basic skill in golang. This article will introduce how to use regular expressions to verify the attribute values ​​​​of XML tags in golang. XML is a markup language commonly used to exchange data on the web. Its syntactic structure is very strict, each element must contain a tag and a set of attributes. When using XML, it is often necessary to verify whether the value of the tag attribute matches

See all articles