PHP Regular Expression: How to match all radio button boxes in HTML
In front-end development, we often need to operate and obtain elements in web pages, and in this process, regular expressions Expressions are a very useful tool. In HTML pages, radio button boxes are one of the common elements, and in this article, we will introduce how to use PHP regular expressions to match all radio button boxes.
First of all, we need to know the structure of the radio button in HTML. A simplest radio button usually contains the following elements:
<label> <input type="radio" name="radio_group"> 选项1 </label>
Wherein, the type
attribute of the <input>
element is radio
, name
The attribute is the name of the radio button group. In the same radio button group, all radio button boxes should have the same name
attribute value, and different radio button boxes should be distinguished by different value
attributes.
Next, we can use PHP’s preg_match_all()
function to match all radio button boxes. The specific code is as follows:
$pattern = '/<input[^>]*type="radio"[^>]*>/i'; $matches = array(); preg_match_all($pattern, $html, $matches);
Among them, the regular expression pattern is /<input[^>]*type="radio"[^>]*>/i
, meaning to match all <input>
elements with the type="radio"
attribute. preg_match_all()
The function will return an array $matches
, which contains all matched elements. We can use the print_r()
function to output this array:
print_r($matches);
The output result is as follows:
Array ( [0] => Array ( [0] => <input type="radio" name="radio_group" value="1"> [1] => <input type="radio" name="radio_group" value="2"> [2] => <input type="radio" name="radio_group" value="3"> [3] => <input type="radio" name="other_group" value="4"> ) )
As you can see, $matches[0]
contains the HTML code of all matching radio button boxes. If we only want to get the name
and value
attribute values of the radio button, we can modify the regular expression to:
$pattern = '/<input[^>]*type="radio"[^>]*name="(.+?)"[^>]*value="(.+?)"[^>]*>/i';
where, $matches[ 1]
is the name
attribute value of the radio button, and $matches[2]
is the value
attribute value. We can get the attribute values of all radio buttons by looping through the array:
foreach ($matches[0] as $key => $value) { preg_match('/<input[^>]*type="radio"[^>]*name="(.+?)"[^>]*value="(.+?)"[^>]*>/i', $value, $match); $name = $match[1]; $value = $match[2]; echo "单选框 $key:name = $name, value = $value<br />"; }
The output result is as follows:
单选框 0:name = radio_group, value = 1 单选框 1:name = radio_group, value = 2 单选框 2:name = radio_group, value = 3 单选框 3:name = other_group, value = 4
In this way, we can use PHP regular expressions to match all radio buttons Frame. Of course, in the actual development process, regular expressions are not omnipotent, and they sometimes cause inaccurate matching. Therefore, we need to use regular expressions carefully according to the specific situation, and we also need to understand some other HTML DOM manipulation methods.
The above is the detailed content of PHP Regular Expression: How to match all radio buttons in HTML. For more information, please follow other related articles on the PHP Chinese website!