Regular expression - solve a PHP regular matching pattern?

WBOY
Release: 2016-08-04 09:19:25
Original
1280 people have browsed it

To grab a page with a data table through cURL, as long as you get all the cells under TR, use the following method

<code>$content = 内容;
preg_match_all('/<td>(.*?)<\/td>/',$content,$res);</code>
Copy after login
Copy after login

But there are two situations that cannot be matched

<code><td>
            
                内容
                
            
        </td></code>
Copy after login
Copy after login

and

<code><td class="sorting_1">
            未付
            
            
            
            
            
            
            
            
            
        </td></code>
Copy after login
Copy after login

I don’t know why cells with a lot of spaces and cells with classes cannot be matched? Since I don’t know how to regularize, I need to find a pattern that can completely match the TD.
There is another question. There are two tables on the page. One is used to filter data under various conditions, and the other is used to hold data. How to match only the table that holds data?

Reply content:

To grab a page with a data table through cURL, as long as you get all the cells under TR, use the following method

<code>$content = 内容;
preg_match_all('/<td>(.*?)<\/td>/',$content,$res);</code>
Copy after login
Copy after login

But there are two situations that cannot be matched

<code><td>
            
                内容
                
            
        </td></code>
Copy after login
Copy after login

and

<code><td class="sorting_1">
            未付
            
            
            
            
            
            
            
            
            
        </td></code>
Copy after login
Copy after login

I don’t know why cells with a lot of spaces and cells with classes cannot be matched? Since I don’t know how to regularize, I need to find a pattern that can completely match the TD.
There is another question. There are two tables on the page. One is used to filter data under various conditions, and the other is used to hold data. How to match only the table that holds data?

.Cannot match newlines in single line mode.
You can use

<code>\s
匹配任意的空白符
\S
匹配任意不是空白符的字符</code>
Copy after login

Learn from one example and match td with class

<code>/<td[\s\S]*?>([\s\S]+?)<\/td>/</code>
Copy after login

To match one of the tables, we can also draw inferences from one example. First, match the table from all the contents into an array.

<code>/<table[\s\S]*?>([\s\S]+?)<\/table>/</code>
Copy after login

Then it depends on which number you want, and then use the above regular matching number.

As I said before, draw inferences from one example!

<code>$pattern = "/<td.*?>([\s\S]*?)<\/td>/";</code>
Copy after login

I’m not very good at it, but I should be able to do it

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template