正则表达式在php中解析

时间:2011-03-19 04:27:23

标签: php regex parsing

  

可能重复:
  how to parse an input value thats hidden

如何使用正则表达式匹配值中的字符?我正在使用php。

<input type="hidden" name="key" value="c126b4f134cb2c1184c1585fdfa4d1b0013a12f4"> 

我试过这个但它没有用

$code = file_get_contents('http://www6.cbox.ws/box/?boxid=522270&boxtag=7xvvk7&sec=form');
preg_match('#<input type="hidden" name="key" value="(.*?)" />#', $code, $match);

2 个答案:

答案 0 :(得分:1)

这与上一个问题(使用DOMDocument)不一样吗?

如果方法太复杂,那么使用phpQuery或QueryPath。这使整个任务变得如此简单:

$url = "http://www6.cbox.ws/box/?boxid=524970&boxtag=7xpsk7&sec=form";
print qp($url)->find("input[type=hidden]")->attr("value");

答案 1 :(得分:0)

<?php 
$in = '<input type="hidden" name="key" value="c126b4f134cb2c1184c1585fdfa4d1b0013a12f4"> ';

$matchs = array();

preg_match("/<input type=\"(\\w+?)\" name=\"(\\w+?)\" value=\"(\\w+?)\">/", $in, $matchs);
var_dump($matchs);

array(4){[0] =&gt; string(81)“”[1] =&gt; string(6)“hidden”[2] =&gt; string(3)“key”[3] =&gt; string(40)“c126b4f134cb2c1184c1585fdfa4d1b0013a12f4”}