Home  >  Article  >  Backend Development  >  Problem with php regular expression html tag matching input, select, textarea

Problem with php regular expression html tag matching input, select, textarea

WBOY
WBOYOriginal
2016-09-28 08:54:101541browse

I want to use regular expressions to match the input, select and textarea tags in the html code. No other tags are needed.
My writing is as follows:

<code>$text = "<form name='loginpageform' method='post' action='www.baidu.com'>";
$pattern="/<.*?[input|textarea|select].*?>/i";
preg_match($pattern1,$text,$matches);
var_dump($matches);</code>

But I found that what I wrote can match all tags. I know it is [input|textarea|select]wrong, but I don’t know how to modify it or if there is an easier way to write it. I hope there is a master. You can answer it.

Reply content:

I want to use regular expressions to match the input, select and textarea tags in the html code, and no other tags are needed.
My writing is as follows:

<code>$text = "<form name='loginpageform' method='post' action='www.baidu.com'>";
$pattern="/<.*?[input|textarea|select].*?>/i";
preg_match($pattern1,$text,$matches);
var_dump($matches);</code>

But I found that what I wrote can match all tags. I know it is [input|textarea|select]wrong, but I don’t know how to modify it or if there is an easier way to write it. I hope there is a master. You can answer it.

  • [] means matching specified characters, not strings

  • (string1|string2) matches multiple strings

  • (?! string1) matches non- string

The correct one is:

<code>/<.*?(input|textarea|select).*?>/is</code>

For example

<code>[a-zA-z0-9_\-]     表示匹配 a~z A~Z 0~9 _ - 这些字符 (只有一个字符)

[a-zA-z0-9_\-]*    表示匹配 a~z A~Z 0~9 _ - 只有这些字符的字符串(注意后面的 * )

(input|textarea|select)  表示匹配这些字符串</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