152124687951<?xml version="1.0"><culo>Amazing</culo></Document>65464614
我只需要提取里面的 XML 代码。我可以有更多的XML代码,我需要一一提取它。它始终以开头。有人可以帮助我吗?谢谢。。。
您可以使用 substr 和 strops 来获取所需的所有匹配项。确实,正则表达式的性能比其他解决方案差。因此,如果性能对您很重要,请考虑其他替代方案。
另一方面,性能可能不是问题(副项目、后台进程等(,因此正则表达式是一种干净的工作方式。
从我的了解中,你有这样的东西:
152124687951<?xml version="1.0"><culo>Amazing</culo></Document>65464614
abc<?xml version="1.0"><culo>Amazing</culo></Document>abc
abc<?xml version="1.0"><culo>Amazing</culo></Document>abc
abc<?xml version="1.0"><culo>Amazing</culo></Document>abc
并且您希望提取其中的所有 xml。因此,一个完美的工作正则表达式将是:
@'<'?xml.+Document'>@
您可以在此处查看实时结果:http://www.regexr.com/39p9q或者你可以在线测试它:https://www.functions-online.com/preg_match_all.html
最后,$matches
变量将具有类似的东西(取决于您在preg_match_all
中使用的缺陷:
array (
0 =>
array (
0 => '<?xml version="1.0"><culo>Amazing</culo></Document>',
1 => '<?xml version="1.0"><culo>Amazing</culo></Document>',
),
)
所以你可以迭代它,仅此而已。
关于性能,这里有一个快速测试:
http://3v4l.org/B1t7h/perf#tabs
鉴于您所描述的上下文,preg_match
可能不是这里的最佳方法。也许以下内容可以更有效地满足您的要求,在执行之前将提供的 XML 示例保存在$sXml中:
$sXml = substr( $sXml, strpos( $sXml, '<?xml' ));
$sXml = substr( $sXml, 0,
strpos( $sXml, '</Document>' ) + strlen( '</Document>' ));
如果您的字符串很大,并且在"XML"部分之后和之前包含许多数据,那么一个好方法(高性能(包括用 strpos
查找开始和结束偏移量,并在之后提取子字符串,例如:
$start = strpos($str, '<?xml ');
$end = strpos(strrev($str), '>tnemucoD/<');
if ($start !== false && $end !== false)
$result = substr($str, $start, - $end);
如果你的字符串不是太大,你可以使用preg_match:
if (preg_match('~'Q<?xml 'E.+?</Document>~s', $str, $m))
$result = $m[0];
'Q....'E
允许编写特殊字符(在正则表达式含义中(,而不必转义它们。(无需提问即可编写文字字符串很有用。但请注意,在本例中,只需要转义?
。