Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand data processing after acquisition based on preg_match_all

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand data processing after data acquisition based on preg_match_all". In daily operation, I believe many people have doubts about how to understand data processing after data acquisition based on preg_match_all. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope to answer the doubts about "how to understand data processing after data acquisition based on preg_match_all".

The copy code is as follows:

/ / Source character is $str

/ / the following known original codes are GBK and converted to utf-8

Mb_convert_encoding ($str, "UTF-8", "GBK")

/ / the following unknown original code is automatically detected by auto, and then converted to utf-8

Mb_convert_encoding ($str, "UTF-8", "auto")

3. In order to better avoid the hindrance of uncertain factors such as newline characters and spaces, it is necessary to remove newline characters, space characters and tabs from the collected source code.

The copy code is as follows:

/ / method 1, replace it with str_replace

$contents = str_replace ("\ r\ n",'', $contents); / / clear the newline character

$contents = str_replace ("\ n",'', $contents); / / clear the newline character

$contents = str_replace ("\ t",'', $contents); / / clear tabs

$contents = str_replace ("",'', $contents); / / clear the space character

/ / method 2, replace it with regular expressions

$contents = preg_replace ("/ ([\ r\ n |\ n |\ t |] +) /",'', $contents)

4. Find out the code snippet you need to get through regular expression matching, and use preg_match_all to achieve the matching.

The copy code is as follows:

Function explanation:

Int preg_match_all (string pattern, string subject, array matches [, int flags])

Pattern is the regular expression

Subject is the original text to search for

Matches is an array used to store output results

Flags is the mode of storage, including:

PREG_PATTERN_ORDER; / / the whole array is a two-dimensional array, $arr1 [0] is an array of matching strings made up of boundaries, and $arr1 [1] is an array of matching strings formed by removing boundaries.

PREG_SET_ORDER; / / the whole array is a two-dimensional array, $arr2 [0] [0] is the first matching string to include the boundary, $arr2 [0] [1] is the first matching string to remove the boundary, and so on.

PREG_OFFSET_CAPTURE / / the whole array is a three-dimensional array, $arr3 [0] [0] [0] is the first matching string including the boundary, $arr3 [0] [0] [1] is the offset to the boundary of the first matching string (excluding the boundary), and so on, $arr2 [1] [0] [0] is the first matching string including the boundary. $arr3 [1] [0] [1] is the offset to the boundary of the first matching string (the boundary is counted)

/ / practical application

Preg_match_all ('/ (. *?) /', $contents, $out, PREG_SET_ORDER)

$out will get all the matching elements

$out [0] [0] will include

The whole paragraph of characters inside

$out [0] [1] will include only (. *?) The character segment matched in parentheses

/ / and so on, the nth matching field can be obtained in the following ways

$out [n-1] [1]

/ / if there are many parentheses in the regular expression, the way to get the m matching point in the sentence is

$out [n-1] [m]

5. After obtaining the characters you want to find, if you want to remove the html tag, you can easily use the function strip_tags that comes with PHP.

The copy code is as follows:

/ / example

$result=strip_tags ($out [0] [1])

At this point, the study on "how to understand data processing after data acquisition based on preg_match_all" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report