我想从字符串中提取一些数据(实际上是HTML)。我搜索了一下,找到了preg_match_all。问题是如果有两个相等的匹配,则该函数将它们视为两个并为两者创造空间。我的代码,代码的输出和代码的预期输出将清楚地解释。
<?php
$html = "
<!doctype html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>Document</title>
</head>
<body>
<img src=\"[profileLink@user:0]\" />
<img src=\"[profileLink@user:0]\" />
</body>
</html>
";
$regex = "/\[(.*?)@(.*?)\]/";
preg_match_all($regex, $html, $matches);
var_dump($matches);
?>
输出:
array (size=3)
0 =>
array (size=2)
0 => string '[profileLink@user:0]' (length=20) // It looks ok
1 => string '[profileLink@user:0]' (length=20) // Don't want it.
1 =>
array (size=2)
0 => string 'profileLink' (length=11) // It looks ok
1 => string 'profileLink' (length=11) // Don't want it.
2 =>
array (size=2)
0 => string 'user:0' (length=6) // It looks ok
1 => string 'user:0' (length=6) // Don't want it.
预期输出:
array (size=3)
0 =>
array (size=1)
0 => string '[profileLink@user:0]' (length=20)
1 =>
array (size=1)
0 => string 'profileLink' (length=11)
2 =>
array (size=1)
0 => string 'user:0' (length=6)
答案 0 :(得分:1)
使用preg_match
代替preg_match_all
,结果将是:
array(3) {
[0]=>
string(20) "[profileLink@user:0]"
[1]=>
string(11) "profileLink"
[2]=>
string(6) "user:0"
}