[求助]正则如何获取文件中的JS数组

php中文网
发布: 2016-06-23 14:20:58
原创
835人浏览过

正则裁切文件 php 数组 JS

有一个需求,读取一个文件抓取文件中的JS数组。
用该正则句仅能抓取到一个值  Array\(.+?\);

而且php中没有仅获取匹配后的数组,所以考虑用 preg_replace 方法将除我想要的数据之外其他数据全部替换掉,最后仅留我需要的部分。

但不知道怎么实现,麻烦大家帮忙咯。

回复讨论(解决方案)

贴出你文件中的数据看看

截掉了部分,要不发不了
var switchPara = new Array(26,0,1,24,25,2,6,8,2,2,4);
var VlanTagGlobalList = new Array(
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 1, 
0, 1, 0, 
0, 0, 99, 
0, 0, 99, 
0,0 );
var VlanTagGlobalTList = new Array(
0, 99, 
0, 99, 
0, 99, 
0, 99, 
0, 99, 
0, 99, 
0,0 );
var userLevel=window.parent.user[1];
//--></Script>
<body><center>
<form action="/userRpm/VlanTagGlobalAd.htm" enctype="multipart/form-data" method="get">
<table class="tphead" width="500" cellspacing=0 cellpadding=0>
<tr><td >Tag VLAN全局配置</td></tr>
</table>
<TABLE class=tpbody cellspacing=0 cellpadding=0 width="500">
<TR><TD align="center"><table width="400" class=tpbodyin>
<TR><TD ><table class=plist>
<tr class=plisttop> <td>端口</td><td>缺省VID</td><td>Untag帧处理 </td><td>端口</td><td>缺省VID</td><td>Untag帧处理 </td></tr>
<script language="JavaScript"><!--
for(i=1;i<=switchPara[0];i=i+2)
{ if(((i+1)/2)%2==1)document.write('<tr>');
else document.write('<tr class=plistsep>');
for(j=i;j<i+2;j++)
{ if(j>switchPara[0])
 break;
 document.write( '<td>'+shiftp(j)+'</td><td><input TYPE=text class=\"text\" SIZE=4 maxlength=4 name=vid></td><td><select name=inr style=\'width:50\'><option value="0" >通过</option><option value="1">丢弃</option>');
if(VlanTagGlobalList[(j-1)*3+2]==99)
document.write('<option value="99" >--</option>');
document.write('</select></td>');
 }
document.write('</tr>');
}
if(userLevel == 1)
{
for(var k = 0; k <= switchPara[0] * 2 - 1; k++)
{
document.forms[0].elements[k].disabled = true;
}
}
var temptrunk;
document.write('<tr></tr><tr></tr>');
for(i=1;i<=switchPara[6];i=i+2)
{if(((i+1)/2)%2==1)document.write('<tr class=plistsep>');
else document.write('<tr>');
for(j=i;j<i+2;j++)
{ temptrunk="T"+j;
 if(j>switchPara[6])
 break;
else
document.write( '<td>'+temptrunk+'</td><td><input TYPE=text class=\"text\" SIZE=4 maxlength=4 name=vidT></td><td><select name=inrT style=\'width:50\'><option value="0" >通过</option><option value="1">丢弃</option>');
if(VlanTagGlobalTList[(j-1)*2+1]==99)
document.write('<option value="99" >--</option>');
 document.write('</select></td>');
 }
document.write('</tr>');
}
if(userLevel == 1)
{
for(k = switchPara[0]*2 ; k <= switchPara[0]*2 + switchPara[6]*2 - 1; k++)
{
document.forms[0].elements[k].disabled = true;
}
}
//--></Script>
<tr><td style='height:10'></td></tr>
<tr>
<td colspan="2"><div align="left">所有端口</div></td>
<td><select name=inrAll style='width:50'onChange="changeinr();">
<option value="99" >--</option>
<option value="0" >通过</option>
<option value="1">丢弃</option>
</select></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</td></tr></table>
</td></tr>
</table>
<table class=tptail cellspacing=0 cellpadding=0 width="500">
<tr><td><INPUT name=submit type=submit class=bbtn value="提交" onClick="return doSubmit();">
  <INPUT name=help type=button class="bbtn" value="帮助" onClick="doHelp('VlanTagGlobalHelp.htm');" >
</td></tr></table>
<script language="JavaScript"><!--
if(switchPara[0]%2==0)
index = switchPara[0]/2;
else
index = (switchPara[0]+1)/2;
if(switchPara[6]%2==0)
indexT = switchPara[6]/2;
else
indexT = (switchPara[6]+1)/2;
for(i=1;i<=index;i++)
{document.write('<input name="p'+i+'" type="hidden">');}
for(i=1;i<=indexT;i++)
{document.write('<input name="t'+i+'" type="hidden">');}
//--></Script>
</form></center></body><head><meta http-equiv="pragma" content="no-cache"></head></html>
<script language="JavaScript"><!--
var DF = document.forms[0];
for( i=1;i<=switchPara[0];i++)
{
betrunk=VlanTagGlobalList[(i-1)*3];
if(betrunk!=0)
{
DF.vid[i-1].value=VlanTagGlobalTList[(betrunk-1)*2];
DF.vid[i-1].disabled=true;
DF.inr[i-1].value=VlanTagGlobalTList[(betrunk-1)*2+1];
DF.inr[i-1].disabled=true;
}
else //betrunk==0 or portdisabled
{
DF.vid[i-1].value =VlanTagGlobalList[(i-1)*3+1];
DF.inr[i-1].value =VlanTagGlobalList[(i-1)*3+2];
if(DF.inr[i-1].value==99)
{
DF.vid[i-1].disabled = true;
DF.inr[i-1].disabled = true;
}
}
}
for( i=1;i<=switchPara[6];i++)
{
if(VlanTagGlobalTList[(i-1)*2+1]==99)
{
DF.vidT[i-1].disabled=true;
DF.inrT[i-1].disabled=true;
}
DF.vidT[i-1].value = VlanTagGlobalTList[(i-1)*2];
DF.inrT[i-1].value = VlanTagGlobalTList[(i-1)*2+1];
}
//--></Script>
<script language = "JavaScript" type = "text/javascript"><!--
if(userLevel == 1)
{
document.forms[0].submit.style.display = "none";
}
//--></script>

$s=你的串preg_match_all('/Array\(.+\)/sU',$s,$m);print_r($m);
登录后复制

$s =<<< TXTvar switchPara = new Array(26,0,1,24,25,2,6,8,2,2,4);var VlanTagGlobalList = new Array(0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 99, 0, 0, 99, 0,0 );var VlanTagGlobalTList = new Array(0, 99, 0, 99, 0, 99, 0, 99, 0, 99, 0, 99, 0,0 );var userLevel=window.parent.user[1];//--></Script>TXT;preg_match_all('/new Array\((.+?)\)/s', $s, $r);print_r($r[1]);
登录后复制
Array
(
    [0] => 26,0,1,24,25,2,6,8,2,2,4
    [1] => 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 1, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 0, 
0, 1, 1, 
0, 1, 0, 
0, 0, 99, 
0, 0, 99, 
0,0 
    [2] => 
0, 99, 
0, 99, 
0, 99, 
0, 99, 
0, 99, 
0, 99, 
0,0 
)

谢谢,十分的感谢。
还是自己的基本不扎实,我还一直以为没有这类函数呢。
看到preg_match_all 仅看到了返回值是int 我就直接放弃看它了。。。
再次感谢

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号