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

正则裁切文件 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];
//-->bedae1ad67868425befe4a2056d27ff6
6c04bd5ca3fcae76e30b72ad730ca86dbacbf9e1ad7f40415ce1670e31edfee3
a9463da6a4d715839f9be19a136dea60
61ca50877680032a8fb127444ae301e5
a34de1251f0d9fe1e645927f19a896e831bff50dc5d62091405569d66bc66752Tag VLAN全局配置b90dd5946f0946207856a8a37f441edffd273fcf5bcad3dfdad3c41bd81ad3e5
f16b1740fad44fb09bfe928bcc527e08
753c122c53ac0bcbbf56642fb95d1d23
f70d0ebff76a19226909a4a79919b5e8d22e5229a386b03ee6104b5d2e79ad26cf6d1f2fd7e053e1ff226b04ef765d70
f70d0ebff76a19226909a4a79919b5e89a8e09710992754b2c624b559648584ea5224fecefb3dc93307c75db52150b1e
bb1fc647f713cf5bd21ef3cc37d3cff6 b6c5a531a458a2e790c1fd6421739d1c端口b90dd5946f0946207856a8a37f441edfb6c5a531a458a2e790c1fd6421739d1c缺省VIDb90dd5946f0946207856a8a37f441edfb6c5a531a458a2e790c1fd6421739d1cUntag帧处理 b90dd5946f0946207856a8a37f441edfb6c5a531a458a2e790c1fd6421739d1c端口b90dd5946f0946207856a8a37f441edfb6c5a531a458a2e790c1fd6421739d1c缺省VIDb90dd5946f0946207856a8a37f441edfb6c5a531a458a2e790c1fd6421739d1cUntag帧处理 b90dd5946f0946207856a8a37f441edffd273fcf5bcad3dfdad3c41bd81ad3e5
c9b2223153d8e39163b0aa064e06fe9ebedae1ad67868425befe4a2056d27ff6
f5a47148e367a6035fd7a2faa965022eadca8a5fa06ffeafb062c2e3f274b93036cc49f0c466276486e50c850b7e495693f0f5c25f18dab9d176bd4f6de5d30e0a38f0830a55e802afea8a264e09be309c3bca370b5104690d9ef395f2c5f8d173a6ac4ed44ffec12cee46588e518a5e
c9b2223153d8e39163b0aa064e06fe9e708689ba0d8b36b6eee3b085345c9d7ebedae1ad67868425befe4a2056d27ff6
a99346057ee270e8e6ad4b11925728138e4efd63b455899fe3bee7f17ed5cd6f2cacc6d41bbb37262a98f745aa00fbf0

$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 我就直接放弃看它了。。。
再次感谢

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment