PHP code that intercepts strings and retains complete xml tags
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:05:41
Original
954 people have browsed it
-
- /**
- * 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(<< ;
- #This regular expression parses xml tags. The tag attribute internally supports the escape character "", and supports the escaping of "" itself and the corresponding quotation marks
- <( w+) #Start of tag
- (?: #Attribute list
- s+ #Preceding space
- w+ #Attribute name
- s* #Blank after attribute name (for compatibility)
- = #Equal sign between attribute name values
- s* #Blank before attribute value (for compatibility)
- (?: #Attribute value (quote processing)
- " #Double quote situation
- (?:
- \\\\ #Eat two consecutive escape characters (indicates escape symbol itself)
- |
- \\" #Eat the escape character followed by a quote (representing an escaped quote)
- |
- [^"\\]* #Other characters
- )*
- "
- |
- ' #Single quote Case
- (?:
- \\\\ #Eat two consecutive escape characters (representing the escape character itself)
- |
- \\' #Eat the escape character followed by a quote (representing the escaped quote)
- |
- [^'\\]* #Other characters
- )*
- '
- )
- )*
- >
- .*? #Tag content
- (?1)> #End tag
- ;x
- heredoc
- , $s, $matches, PREG_OFFSET_CAPTURE, $o);
- if ( $is_match ) {
- foreach ( $matches[0] as $match ) {
- $o0 = $match[1];
- #The label left boundary is intercepted when it crosses Right boundary of the target, exit
- if ( $o0 >= $o + $l ) break;
- $l0 = strlen($match[0]);
- #The right boundary of the label is within the right boundary of the intercepted target, continue
- if ( $o0 + $l0 < $o + $l ) continue;
-
- #The following is label cross-border processing
- $l = $o0 + $l0 - $o;
- break;
- }
- }
- return substr($s , $o, $l);
- }
- echo $str . chr(10);
- echo substr_remain_tag($str, 0, 20) . chr(10);
Copy code
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31