Php 使用RegEx提取具有特定输出的特定数据
我需要一个正则表达式来提取指定的值,但不起作用 下面是HTML代码:Php 使用RegEx提取具有特定输出的特定数据,php,regex,Php,Regex,我需要一个正则表达式来提取指定的值,但不起作用 下面是HTML代码: <body style="background: #FFF; padding-left: 5px;"> <form name="form1" method="post" action="verify()" id="form1"> <div> <input type="hidden" name="__VIEWSTATE" id="__VIE
<body style="background: #FFF; padding-left: 5px;">
<form name="form1" method="post" action="verify()" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/asdasfafasf/9Q2w==" />
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKb/LCHCALs0bLrBgKM54rGBulKe8VRM9SNhTfqyz0GubMFea7i" />
</div>
<div class="nicer">
<input name="TextBox1" type="text" value="asdf44" id="TextBox1" placeholder="Ingresa tu patente" />
</div>
<p class="sample">
<br /> sample: asdasd34 ó ABCD12
<br /> Para . Ej. AB<strong style="font-weight: bold !importand;">0</strong>123</p>
<p>
<input type="submit" name="Button1" value="Consultar" id="Button1" class="button orange_btn small_btn" />
</p>
<h3><span id="Label1" class="infractions_report">result: asdf44</span></h3>
<div>
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col">date</th>
<th scope="col">category</th>
<th scope="col">statusok</th>
</tr>
<tr class="txt">
<td>10-08-2015</td>
<td>1</td>
<td>cs nor</td>
</tr>
<tr class="txt">
<td>04-08-2015</td>
<td>1</td>
<td>cs nor2</td>
</tr>
<tr class="txt">
<td>01-08-2015</td>
<td>1</td>
<td>cs nor3</td>
</tr>
<tr class="txt">
<td>30-07-2015</td>
<td>1</td>
<td>cs nor4</td>
</tr>
<tr class="txt">
<td>19-06-2015</td>
<td>1</td>
<td>cn nor5</td>
</tr>
</table>
</div>
</form>
</body>
$expresiondate = '/\<tr\>[\s]*\<td class\=\"txt\"\>[\s]*([^\s\<\/]*)/is';
preg_match_all($expresiondate , $buffer, $exit1);
$expresionCategory= '/\-[\d]{4}[\s]*<\/td\>[\s]*\<td class\=\"txt\"\>[\s]*([^\s\<\/]*)/is';
preg_match_all($expresionCategory, $buffer, $exit2);
$expresionstatus= '/\>[\s]*[\d]*[\s]*<\/td\>[\s]*\<td class\=\"txt\"\>[\s]*([^\s\<\/]*)/is';
preg_match_all($expresionstatus, $buffer, $exit3);
样本:asdasd34ó;ABCD12
第。Ej。AB0123
结果:asdf44
日期
类别
状态OK
10-08-2015
1.
cs nor
04-08-2015
1.
政务司司长2
01-08-2015
1.
政务司司长3
30-07-2015
1.
政务司司长4
19-06-2015
1.
中国五号
下面是PHP代码:
<body style="background: #FFF; padding-left: 5px;">
<form name="form1" method="post" action="verify()" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/asdasfafasf/9Q2w==" />
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKb/LCHCALs0bLrBgKM54rGBulKe8VRM9SNhTfqyz0GubMFea7i" />
</div>
<div class="nicer">
<input name="TextBox1" type="text" value="asdf44" id="TextBox1" placeholder="Ingresa tu patente" />
</div>
<p class="sample">
<br /> sample: asdasd34 ó ABCD12
<br /> Para . Ej. AB<strong style="font-weight: bold !importand;">0</strong>123</p>
<p>
<input type="submit" name="Button1" value="Consultar" id="Button1" class="button orange_btn small_btn" />
</p>
<h3><span id="Label1" class="infractions_report">result: asdf44</span></h3>
<div>
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col">date</th>
<th scope="col">category</th>
<th scope="col">statusok</th>
</tr>
<tr class="txt">
<td>10-08-2015</td>
<td>1</td>
<td>cs nor</td>
</tr>
<tr class="txt">
<td>04-08-2015</td>
<td>1</td>
<td>cs nor2</td>
</tr>
<tr class="txt">
<td>01-08-2015</td>
<td>1</td>
<td>cs nor3</td>
</tr>
<tr class="txt">
<td>30-07-2015</td>
<td>1</td>
<td>cs nor4</td>
</tr>
<tr class="txt">
<td>19-06-2015</td>
<td>1</td>
<td>cn nor5</td>
</tr>
</table>
</div>
</form>
</body>
$expresiondate = '/\<tr\>[\s]*\<td class\=\"txt\"\>[\s]*([^\s\<\/]*)/is';
preg_match_all($expresiondate , $buffer, $exit1);
$expresionCategory= '/\-[\d]{4}[\s]*<\/td\>[\s]*\<td class\=\"txt\"\>[\s]*([^\s\<\/]*)/is';
preg_match_all($expresionCategory, $buffer, $exit2);
$expresionstatus= '/\>[\s]*[\d]*[\s]*<\/td\>[\s]*\<td class\=\"txt\"\>[\s]*([^\s\<\/]*)/is';
preg_match_all($expresionstatus, $buffer, $exit3);
$expresiondate='/\[\s]*\[\s]*([^\s\正则表达式很难解释
我建议使用“命名捕获组”从表单元格中刮取内容
我提出了以下regexp:
$regexp = "/<td>(?P<data>(\d{2}-\d{2}-\d{4}))<\/td>\s+<td>(?P<category>\d{1})<\/td>\s+<td>(?P<status>.*)<\/td>/mi";
preg_match_all($regexp, $input_lines, $matches);
$regexp=“/(?P(\d{2}-\d{2}-\d{4}))\s+(?P\d{1})\s+(?P.*)/mi”;
preg_match_all($regexp、$input_line、$matches);
乍一看,那东西可能看起来势不可挡,但它是由部分组成的
好的,让我们一个接一个地看一下:
- 捕获组以
(?P(regexp))
- 其中regexp是要为该组捕获的部分
- 您希望捕获td标签中的内容,因此可以将组放置在td标签中
- 每个td标记后面都有空格
\s+
- 现在,捕获组的3个regexp部分
- 日期
(\d{2}-\d{2}-\d{4})
- 匹配:两位数减两位数减4位数
- 捕获组=
(?P(\d{2}-\d{2}-\d{4}))
- 类别
\d{1}
- 只有一位数
- 捕获组=
(?P\d{1})
- 状态
*
- 全部匹配,混合
- 捕获组=
(?P.*)
运行preg_match_all
justvar_dump($matches);
后,它应该包含日期、类别和状态的键
永远记住:只有Chuck Norris可以用正则表达式解析HTML。正则表达式很难解释
我建议使用“命名捕获组”从表单元格中刮取内容
我提出了以下regexp:
$regexp = "/<td>(?P<data>(\d{2}-\d{2}-\d{4}))<\/td>\s+<td>(?P<category>\d{1})<\/td>\s+<td>(?P<status>.*)<\/td>/mi";
preg_match_all($regexp, $input_lines, $matches);
$regexp=“/(?P(\d{2}-\d{2}-\d{4}))\s+(?P\d{1})\s+(?P.*)/mi”;
preg_match_all($regexp、$input_line、$matches);
乍一看,那东西可能看起来势不可挡,但它是由部分组成的
好的,让我们一个接一个地看一下:
- 捕获组以
(?P(regexp))
- 其中regexp是要为该组捕获的部分
- 您希望捕获td标签中的内容,因此可以将组放置在td标签中
- 每个td标记后面都有空格
\s+
- 现在,捕获组的3个regexp部分
- 日期
(\d{2}-\d{2}-\d{4})
- 匹配:两位数减两位数减4位数
- 捕获组=
(?P(\d{2}-\d{2}-\d{4}))
- 类别
\d{1}
- 只有一位数
- 捕获组=
(?P\d{1})
- 状态
*
- 全部匹配,混合
- 捕获组=
(?P.*)
运行preg_match_all
justvar_dump($matches);
后,它应该包含日期、类别和状态的键
永远记住:只有Chuck Norris可以用正则表达式解析HTML。再一次,正则表达式不是解析HTML的工具。请使用专门为其设计的内置工具DOMDocument
和DOMXPath
$url = 'page.html';
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile($url);
$xp = new DOMXPath($dom);
$rowNodeList = $xp->query('//table[@id="GridView1"]/tr[@class="txt"]');
$results = [];
foreach ($rowNodeList as $rowNode) {
$colNodeList = $rowNode->getElementsByTagName('td');
$results[] = [ 'date' => $colNodeList->item(0)->nodeValue,
'category' => $colNodeList->item(1)->nodeValue,
'status' => $colNodeList->item(2)->nodeValue ];
}
libxml_clear_errors();
print_r($results);
还有一次,regex不是解析HTML的工具。请使用专门为其设计的内置工具DOMDocument
和DOMXPath
$url = 'page.html';
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile($url);
$xp = new DOMXPath($dom);
$rowNodeList = $xp->query('//table[@id="GridView1"]/tr[@class="txt"]');
$results = [];
foreach ($rowNodeList as $rowNode) {
$colNodeList = $rowNode->getElementsByTagName('td');
$results[] = [ 'date' => $colNodeList->item(0)->nodeValue,
'category' => $colNodeList->item(1)->nodeValue,
'status' => $colNodeList->item(2)->nodeValue ];
}
libxml_clear_errors();
print_r($results);
我可以解析带正则表达式的html;)look>/\\s*(.*)(.*)(.*)(.*)/mi;)很高兴我可以帮助一个bitpd我可以解析带正则表达式的html;)look>/\\s*(.*)(.*)(.*)/mi;)很高兴我可以帮助一点