Php function code for intercepting strings and retaining the complete xml tag

Source: Internet
Author: User
The php code that intercepts the string and retains the complete xml tag. if you need it, refer The code is as follows:


/**
* Author: goosman
* Blog: http://blog.csdn.net/lgg201
* Mail: lgg860911@yahoo.com.cn
*/

$ Str = '000000 ';
Function substr_remain_tag ($ s, $ o, $ l ){
$ Is_match = preg_match_all (<

# This regular expression parses xml tags. the tag attributes support the escape character "\" and escape the "\" itself and corresponding quotation marks.
<(\ W +) # label start
(? : # Attribute list
\ S + # leading space
\ W + # attribute name
\ S * # blank space after attribute name (for compatibility)
= # Equal sign between attribute name values
\ S * # blank before attribute values (for compatibility)
(? : # Attribute values (quotes)
"# Double quotation marks
(? :
\\\\\\\\# Eat two consecutive escape characters (indicating the escape character itself)

\\\\ "# Eat the escape character followed by a quotation mark (indicating the escape quotation mark)

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

'# Single quotes
(? :
\\\\\\\\# Eat two consecutive escape characters (indicating the escape character itself)

\\\\' # Eat the escape character followed by a quotation mark (indicating the escape quotation mark)

[^ '\\\\] * # Other characters
)*
'
)
)*
>
.*? # Tag content
# End tag
; X
Heredoc
, $ S, $ matches, PREG_OFFSET_CAPTURE, $ o );
If ($ is_match ){
Foreach ($ matches [0] as $ match ){
$ O0 = $ match [1];
# The left boundary of the tag crosses the right boundary of the capture target and exits.
If ($ o0 >=$ o + $ l) break;
$ L0 = strlen ($ match [0]);
# The right boundary of the tag is within the right boundary of the captured target. continue
If ($ o0 + $ l0 <$ o + $ l) continue;

# Tag 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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.