在 Php 正则表达式上提取错误字符串(preg_match_all 函数)
我有一个数组:
$arr1[0]=SOAP-ENV:Server Market 1
$arr[1]="";
$arr[2]=0;
$arr[3]="|Error : User Name and Password is Failed"
第二个只是普通变量: K i 合并:
$collarr = array_merged($var1,$othervar);
print_r($collarr);
$collarr 结果 :
Array (
[0] => SOAP-ENV:Server Market 1|
[1] =>
[2] => 0
[3] => |Error : User Name and Password is Failed
[4] => SOAP-ENV:Server Market 2| 0 |Error : Failed Mysql query())
我用正则表达式解析
preg_match_all("/[error]+/i",$collarr[0].$collarr[4],$resultarr);
BUT i只需得到结果:
Array ( [0] => Array ( [0] => Error [1] => Error ) )
获得结果如的最佳正则表达式模式是什么:
"Error Soap-Env:Server market 1: User Name and Password is Failed Server Market"."Error Soap-Env:Server market 2 : Failed Mysql query()"
谢谢。
I have an array:
$arr1[0]=SOAP-ENV:Server Market 1
$arr[1]="";
$arr[2]=0;
$arr[3]="|Error : User Name and Password is Failed"
the second just normal variable:
K
i merge:
$collarr = array_merged($var1,$othervar);
print_r($collarr);
$collarr result :
Array (
[0] => SOAP-ENV:Server Market 1|
[1] =>
[2] => 0
[3] => |Error : User Name and Password is Failed
[4] => SOAP-ENV:Server Market 2| 0 |Error : Failed Mysql query())
i parse with regex
preg_match_all("/[error]+/i",$collarr[0].$collarr[4],$resultarr);
BUT i just get the result :
Array ( [0] => Array ( [0] => Error [1] => Error ) )
What is the best regex pattern to have for have result like:
"Error Soap-Env:Server market 1: User Name and Password is Failed Server Market"."Error Soap-Env:Server market 2 : Failed Mysql query()"
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下函数 preg_grep,它过滤出指定模式的数组:
为我打印:
Look at the funciont preg_grep, it filters out the array for the specified pattern:
Prints for me:
您可以使用包含
preg_match_all()
和preg_grep()
的方法,但这些函数使用正则表达式,通常比非正则表达式效率低 -为基础的函数。如果您始终具有与示例数组中提供的相同的数据结构,那么我有一个使用 array_filter(), implode()< /a> 和 str_replace() 为每个错误数组提供所需的字符串。
这将输出:
您会看到,如果将 4 元素数组提供给我的 3 函数方法,它将首先删除空/空/零值,然后使用空格作为粘合将剩余元素组合到将被剥离的字符串中它的所有管道字符。
如果您打算对这些数据进行任何串联/组合/合并,我建议您在生成所需的字符串后执行此操作。
如果您的实际案例具有更多样化的值,并且我的答案无法适应您项目的复杂性,请使用一些导致麻烦的示例输入更新您的问题,我将调整我的方法。
You could use methods that include
preg_match_all()
andpreg_grep()
, but those function use regex and are generally less efficient than non-regex-based functions.If you always have the same data structure that you provided in your sample arrays, then I have a one-liner solution that uses array_filter(), implode(), and str_replace() to provide your desired string for each error array.
This will output:
You see that if you feed the 4-element array to my 3-function method, it will first remove empty/null/zero values, then combine the remaining elements using a space as glue into a string that will be stripped of all of its pipe characters.
If you are going to do any concatenation/combination/merging of this data, I recommend doing so after the desired string has been generated.
If your actual case has more diverse values and my answer doesn't accommodate your project's complexity, please update your question with some sample input that causes trouble and I'll adjust my method.