日期:2014-05-17 浏览次数:20430 次
$str = <<<HTML <input type="hidden" name="idArticleslist" value="2425898,2427913,"><input type="hidden" id="firstidarticle" name="firstidarticle" value="2425898"><input type="hidden" name="pageflag" value="1"> </form> </div> HTML; if (preg_match('/idArticleslist"\svalue="([\d\,]+)"/', $str, $match)) { echo $match[1]; }
------解决方案--------------------
[liangdong@bb-browser-test00.vm.baidu.com php_project]$ php main.php Array ( [0] => 2425898 [1] => 2427913 ) [liangdong@bb-browser-test00.vm.baidu.com php_project]$ cat main.php <?php $str = <<<EOF <input type="hidden" name="idArticleslist" value="2425898,2427913,"><input type="hidden" id="firstidarticle" name="firstidarticle" value="2425898"><input type="hidden" name="pageflag" value="1"> </form> </div> EOF; $n = preg_match('/<input.*name="idArticleslist".*value="(.*)"/iUs', $str, $matches); if ($n) { print_r(explode(',', trim($matches[1], ','))); } ?>
------解决方案--------------------
$s=<<<html <input type="hidden" name="idArticleslist" value="2425898,2427913,"><input type="hidden" id="firstidarticle" name="firstidarticle" value="2425sss98"><input type="hidden" name="pageflag" value="1"> </form> </div> html; preg_match('#(?<=idArticleslist).+?value="([^\"]+)"#s',$s,$m); echo $m[1]; // 2425898,2427913,
------解决方案--------------------
<input type="hidden" name="idArticleslist" value="2425898,2427913,">
正则:
<input type=\"hidden\" name=\"idArticleslist\" value=\"(.[^"]*)\">
正则就这么简单,需要转译的加一个反斜线,要匹配的都在括号里,防止匹配的太贪婪,你就告诉他[^"],意思是遇到双引号就匹配结束.这样就行了
给你推荐一个软件 RegexBuddy 这个软件用来测试正则表达式的,你可以借助它学一下正则,写程序不会正则是很麻烦的.