html代码很多这个是其中的两句
<input type='hidden' name='comment_post_ID' value='28' id='comment_post_ID' />
<form action="http://www.baidu.com/b.php" method="post" id="commentform">
求提取出
第一句里面的value的值 28和第二句里面的http://www.baidu.com/b.php
第1个回答 2012-12-14
完整代码:
<?php
/*
Function:
关于PHP的正则表达式。求帮忙。。。
Author: Crifan Li
Contact: admin (at) crifan (dot) com
*/
$inputHtml = <<<crifan
<input type='hidden' name='comment_post_ID' value='28' id='comment_post_ID' />
<form action="" method="post" id="commentform">
crifan;
$valueActionP = "#<input.+?value='(?<value>\d+)'.+?/>.*<form action=\"(?<action>)\".+>#is";
preg_match($valueActionP, $inputHtml, $matches);
//print_r($matches);
print_r($matches["value"]."<br />");
print_r($matches["action"]."<br />");
?>
就可以打印出:
28
另外,感兴趣的话,可以去看我总结的:
PHP中的正则表达式的学习心得
(此处不给贴地址,请自己用google搜标题,即可找到帖子地址)
<?php
/*
Function:
关于PHP的正则表达式。求帮忙。。。
Author: Crifan Li
Contact: admin (at) crifan (dot) com
*/
$inputHtml = <<<crifan
<input type='hidden' name='comment_post_ID' value='28' id='comment_post_ID' />
<form action="" method="post" id="commentform">
crifan;
$valueActionP = "#<input.+?value='(?<value>\d+)'.+?/>.*<form action=\"(?<action>)\".+>#is";
preg_match($valueActionP, $inputHtml, $matches);
//print_r($matches);
print_r($matches["value"]."<br />");
print_r($matches["action"]."<br />");
?>
就可以打印出:
28
另外,感兴趣的话,可以去看我总结的:
PHP中的正则表达式的学习心得
(此处不给贴地址,请自己用google搜标题,即可找到帖子地址)
第2个回答 2012-12-12
'/value='([0-9]+)'/'
'/action="(.+)"/'
用preg_macth_all()来取子模式追问
'/action="(.+)"/'
用preg_macth_all()来取子模式追问
id='comment_post_ID'
method="post"
要把这2个放进表达式里面
因为value='不是唯一的
第3个回答 2012-12-14
value='([^']+)'[^>]*id='comment_post_ID'
action="([^"]+)"[^>]*id="commentform"
action="([^"]+)"[^>]*id="commentform"