Php preg_match_all未给出预期结果
我有一个像这样的HTML内容Php preg_match_all未给出预期结果,php,preg-match-all,Php,Preg Match All,我有一个像这样的HTML内容 some html text [link id="1"] some html text some html text [link id="2"] some html text some html text [link id="3" stat="y"] some html text some html text [link id="4" stat="y"] some html text 我正在使用 var\u dump(preg\u match\u
some html text [link id="1"] some html text
some html text [link id="2"] some html text
some html text [link id="3" stat="y"] some html text
some html text [link id="4" stat="y"] some html text
我正在使用
var\u dump(preg\u match\u all('/\[link([^\[]+)\]/',$html\u tags,$result));
要从中提取[link]
标记,它会给我一个数组
array(2) {
[0]=> array(4) {
[0]=> string(13) "[link id="1"]"
[1]=> string(13) "[link id="2"]"
[2]=> string(21) "[link id="3" stat="y"]"
[3]=> string(21) "[link id="4" stat="y"]"
}
[1]=> array(4) {
[0]=> string(7) " id="1""
[1]=> string(7) " id="2""
[2]=> string(15) " id="3" stat="y""
[3]=> string(15) " id="4" stat="y""
}
}
谁能告诉我怎样才能得到这样的结果
array (1){
[0]=> array (4) {
[0] => array (2){
[id] => string(1) "1"
[stat] => string(0) ""
}
[1] => array (2){
[id] => string(1) "2"
[stat] => string(0) ""
}
[2] => array (2){
[id] => string(1) "3"
[stat] => string(1) "y"
}
[3] => array (2){
[id] => string(1) "4"
[stat] => string(1) "y"
}
}
}
您可以使用自定义编写的函数和数组映射
这应该对你有用。获取meta\u标签($html\u标签)
确实达到了我想要的效果,但如何使用它获取[link]
tags?你真是太棒了…非常感谢你完成了这个脚本..非常感谢:)第二个函数有一些错误,总是给我一个带有假值的数组,你能修复它吗array(2){[0]=>array(2){[[id]=>bool(false)[“stat”]=>bool(false)}[1]=>array(2){[[id]=>bool(false)[“stat”]=>string(9)“sub=“aa”“}}
//first flatten array
$array = $array[1];
array_map("passone", $array);
array_map("passtwo", $array);
function passone ($n) {
return substr($n, 1);
}
function passtwo ($n) {
$nx = explode(" ", $n);
preg_match("link=\"(.*?)\"", $nx[0], $matches);
$ret['id'] = $matches[0];
if (!empty($nx[1]))
{
$times = preg_match("stat=\"(.*?)\"", $nx[1], $matches);
if ($times != 0)
{
$ret['stat'] = $matches[0];
}
}
return $ret;
}