Home  >  Article  >  Backend Development  >  Regular expression - solve a PHP regular matching pattern?

Regular expression - solve a PHP regular matching pattern?

WBOY
WBOYOriginal
2016-08-04 09:19:251240browse

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>

But there are two situations that cannot be matched

<code><td>
            
                内容
                
            
        </td></code>

and

<code><td class="sorting_1">
            未付
            
            
            
            
            
            
            
            
            
        </td></code>

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>

But there are two situations that cannot be matched

<code><td>
            
                内容
                
            
        </td></code>

and

<code><td class="sorting_1">
            未付
            
            
            
            
            
            
            
            
            
        </td></code>

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>

Learn from one example and match td with class

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

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>

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>

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

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