search
HomeBackend DevelopmentPHP TutorialPHP code that intercepts strings and retains complete xml tags

  1. /**
  2. * author: goosman
  3. * blog: http://blog.csdn.net/lgg201
  4. * mail: lgg860911@yahoo.com.cn
  5. */
  6. $str = '01234567890120123456789';
  7. function substr_remain_tag($s, $o, $l) {
  8. $is_match = preg_match_all( ;
  9. #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
  10. (?: #Attribute list
  11. s+ #Preceding space
  12. w+ #Attribute name
  13. s* #Blank after attribute name (for compatibility)
  14. = #Equal sign between attribute name values
  15. s* #Blank before attribute value (for compatibility)
  16. (?: #Attribute value (quote processing)
  17. " #Double quote situation
  18. (?:
  19. \\\\ #Eat two consecutive escape characters (indicates escape symbol itself)
  20. |
  21. \\" #Eat the escape character followed by a quote (representing an escaped quote)
  22. |
  23. [^"\\]* #Other characters
  24. )*
  25. "
  26. |
  27. ' #Single quote Case
  28. (?:
  29. \\\\ #Eat two consecutive escape characters (representing the escape character itself)
  30. |
  31. \\' #Eat the escape character followed by a quote (representing the escaped quote)
  32. |
  33. [^'\\]* #Other characters
  34. )*
  35. '
  36. )
  37. )*
  38. >
  39. .*? #Tag content
  40. (?1)> #End tag
  41. ;x
  42. heredoc
  43. , $s, $matches, PREG_OFFSET_CAPTURE, $o);
  44. if ( $is_match ) {
  45. foreach ( $matches[0] as $match ) {
  46. $o0 = $match[1];
  47. #The label left boundary is intercepted when it crosses Right boundary of the target, exit
  48. if ( $o0 >= $o + $l ) break;
  49. $l0 = strlen($match[0]);
  50. #The right boundary of the label is within the right boundary of the intercepted target, continue
  51. if ( $o0 + $l0
  52. #The following is label cross-border processing
  53. $l = $o0 + $l0 - $o;
  54. break;
  55. }
  56. }
  57. return substr($s , $o, $l);
  58. }
  59. echo $str . chr(10);
  60. echo substr_remain_tag($str, 0, 20) . chr(10);
Copy code


Statement
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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Framework Security Features: Protecting against vulnerabilities.Framework Security Features: Protecting against vulnerabilities.Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool