正则表达式 xml 取出多个详细的值
这是xml的部分值,请问要如何依次取出相应的值?
比如将1A0003、1B0001放在一个数组里,
B股指数、工业指数放在一个数组里,
BGZS、GYZS放在一个数组。
谢谢各位大牛,
preg_match_all使用三次么?
该怎么写这样的表达式?
------解决思路----------------------
使用正则捕获功能。
<br /><?php <br />$s = '<PY>1A0003<br><font color='#FF8000'>------解决思路----------------------</font><br>B股指数<br><font color='#FF8000'>------解决思路----------------------</font><br>BGZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0001<br><font color='#FF8000'>------解决思路----------------------</font><br>工业指数<br><font color='#FF8000'>------解决思路----------------------</font><br>GYZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0002<br><font color='#FF8000'>------解决思路----------------------</font><br>商业指数<br><font color='#FF8000'>------解决思路----------------------</font><br>SYZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0004<br><font color='#FF8000'>------解决思路----------------------</font><br>地产指数<br><font color='#FF8000'>------解决思路----------------------</font><br>DCZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0005<br><font color='#FF8000'>------解决思路----------------------</font><br>公用指数<br><font color='#FF8000'>------解决思路----------------------</font><br>GYZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0006<br><font color='#FF8000'>------解决思路----------------------</font><br>综合指数<br><font color='#FF8000'>------解决思路----------------------</font><br>ZHZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY>';<br /><br />preg_match_all('/<PY>([^<br><font color='#FF8000'>------解决思路----------------------</font><br>]*)\<br><font color='#FF8000'>------解决思路----------------------</font><br>([^<br><font color='#FF8000'>------解决思路----------------------</font><br>]*)\<br><font color='#FF8000'>------解决思路----------------------</font><br>([^<br><font color='#FF8000'>------解决思路----------------------</font><br>]*)\<br><font color='#FF8000'>------解决思路----------------------</font><br>([^<]*)<\/PY>/', $s, $matches);<br />print_r($matches[1]);<br />print_r($matches[2]);<br />print_r($matches[3]);<br />print_r($matches[4]);<br /><br />
结果:
Array
(
[0] => 1A0003
[1] => 1B0001
[2] => 1B0002
[3] => 1B0004
[4] => 1B0005
[5] => 1B0006
)
Array
(
[0] => B股指数
[1] => 工业指数
[2] => 商业指数
[3] => 地产指数
[4] => 公用指数
[5] => 综合指数
)
Array
(
[0] => BGZS
[1] => GYZS
[2] => SYZS
[3] => DCZS
[4] => GYZS
[5] => ZHZS
)
Array
(
[0] => 16
[1] => 16
[2] => 16
[3] => 16
[4] => 16
[5] => 16
)
------解决思路----------------------
<br />$str = <<<EOF<br /><root><br /><PY>1A0003<br><font color='#FF8000'>------解决思路----------------------</font><br>B股指数<br><font color='#FF8000'>------解决思路----------------------</font><br>BGZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0001<br><font color='#FF8000'>------解决思路----------------------</font><br>工业指数<br><font color='#FF8000'>------解决思路----------------------</font><br>GYZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0002<br><font color='#FF8000'>------解决思路----------------------</font><br>商业指数<br><font color='#FF8000'>------解决思路----------------------</font><br>SYZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0004<br><font color='#FF8000'>------解决思路----------------------</font><br>地产指数<br><font color='#FF8000'>------解决思路----------------------</font><br>DCZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0005<br><font color='#FF8000'>------解决思路----------------------</font><br>公用指数<br><font color='#FF8000'>------解决思路----------------------</font><br>GYZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /><PY>1B0006<br><font color='#FF8000'>------解决思路----------------------</font><br>综合指数<br><font color='#FF8000'>------解决思路----------------------</font><br>ZHZS<br><font color='#FF8000'>------解决思路----------------------</font><br>16</PY><br /></root><br />EOF;<br /><br />$s = simplexml_load_string($str);<br />foreach($s->xpath("//PY") as $k=>$v){<br /> list($a[],$b[],$c[])=explode('<br><font color='#FF8000'>------解决思路----------------------</font><br>',$v);<br />}<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($a);<br />print_r($b);<br />print_r($c);<br />echo "";
/*
Array
(
[0] => 1A0003
[1] => 1B0001
[2] => 1B0002
[3] => 1B0004
[4] => 1B0005
[5] => 1B0006
)
Array
(
[0] => B股指数
[1] => 工业指数
[2] => 商业指数
[3] => 地产指数
[4] => 公用指数
[5] => 综合指数
)
Array
(
[0] => BGZS
[1] => GYZS
[2] => SYZS
[3] => DCZS
[4] => GYZS
[5] => ZHZS
)
*/

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

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.

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

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

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

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

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
