如何过滤我的内容并在其他地方一起显示这些值。内容通过 XML 加载,并由我们的客户端填充。因此我无法更改这些值。问题是我的价值观不同。我目前正在过滤一些值:
if (!empty($arraywithvalues)) {
$array = explode("<br>", $arraywithvalues);
foreach( $array as $key => $value){
if (
strpos(strtolower($value),'oppervlakte') !== FALSE ||
strpos(strtolower($value),'perceel') !== FALSE ||
strpos(strtolower($value),'golf') !== FALSE
) {
$filtered_content[] = $value;
unset($array[$key]);
}
};
这是为了过滤掉文本位,这已经成功完成,可以在这个网站上看到。 问题是仍然有一些值需要过滤掉,例如:
20M2
20公里
不过过滤本身是没有问题的。我遇到的问题是将这些值放在一个或另一个旁边。 例如。距高尔夫球场的距离(第一个过滤值)- 20 公里(第二个过滤值)。我怎样才能实现这个目标?
导入时的 XML:
string(82)"
Bebouwde oppervlakte
80 M2
Terras oppervlakte
20 M2
Keuken oppervlakte
12 M2"
PHP8
<?php
$values = <<<EOL
Bebouwde oppervlakte
80 M2
Terras oppervlakte
20 M2
Keuken oppervlakte
12 M2
EOL;
if (!$values) {
// you can throw an exception and/or log etc.
return;
}
$valueList = array_filter(explode(PHP_EOL, $values));
$valueList = array_values(array_map('trim', $valueList));
$formatedValueList = [];
foreach($valueList as $key => $value) {
if ((
str_contains(strtolower($value), 'oppervlakte')
|| str_contains(strtolower($value), 'perceel')
|| str_contains(strtolower($value), 'golf')
) && (
str_contains(strtolower($valueList[$key+1]), 'm2')
|| str_contains(strtolower($valueList[$key+1]), 'km')
)
) {
$formatedValueList[] = sprintf('%s - %s', $value, $valueList[$key+1]);
}
}
var_dump($formatedValueList);
PHP7
<?php
$values = <<<EOL
Bebouwde oppervlakte
80 M2
Terras oppervlakte
20 M2
Keuken oppervlakte
12 M2
EOL;
if (!$values) {
// you can throw an exception and/or log etc.
return;
}
$valueList = array_filter(explode(PHP_EOL, $values));
$valueList = array_values(array_map('trim', $valueList));
$formatedValueList = [];
foreach($valueList as $key => $value) {
if ((
strpos(strtolower($value), 'oppervlakte') !== false
|| strpos(strtolower($value), 'perceel') !== false
|| strpos(strtolower($value), 'golf') !== false
) && (
strpos(strtolower($valueList[$key+1]), 'm2') !== false
|| strpos(strtolower($valueList[$key+1]), 'km') !== false
)
) {
$formatedValueList[] = sprintf('%s - %s', $value, $valueList[$key+1]);
}
}
var_dump($formatedValueList);
输出
array(3) {
[0]=>
string(28) "Bebouwde oppervlakte - 80 M2"
[1]=>
string(26) "Terras oppervlakte - 20 M2"
[2]=>
string(26) "Keuken oppervlakte - 12 M2"
}
只要记录按上面的顺序排列就相当容易了。您缓冲第一行,然后继续读取,直到出现第二个值的行。然后连接缓冲区和当前值。
if (!empty($arraywithvalues)) {
$array = explode("<br>", $arraywithvalues);
foreach( $array as $value){
if ( strpos(strtolower($value),'oppervlakte') !== FALSE ||
strpos(strtolower($value),'perceel') !== FALSE ||
strpos(strtolower($value),'golf') !== FALSE ) {
$buffer = $value;
} elseif ( strpos(strtolower($value),'m2') !== FALSE ||
strpos(strtolower($value),'km') !== FALSE ) {
$filtered_content[] = $buffer . ' - ' . $value;
}
}
}
有点草率,但我认为它完成了工作:
<?php
$stringArray = explode("\r\n", trim($arraywithvalues));
$stringArray = array_filter($stringArray);
$stringArray = array_values($stringArray);
$newArray = [];
foreach($stringArray as $key => $value) {
if($key % 2 == 0)
$newArray[$value] = '';
else
$newArray[$stringArray[$key-1]] = $value;
}
//var_dump($newArray);
?>
最后你会得到一个带有 key=>value 的数组,如下所示:
[
"Bebouwde oppervlakte" => "80 M2"
"Terras oppervlakte" => "20 M2"
"Keuken oppervlakte" => "12 M2"
]
您可以循环播放并根据需要显示
由于您的示例输入文本仅包含要保留的文本,因此只需使用
preg_match_all()
捕获连续的文本对,然后使用 array_column()
将这些对形成关联数组。 (演示)
preg_match_all(
'/(\S+(?: \S+)*)\s+(\S+(?: \S+)*)/',
$text,
$m,
PREG_SET_ORDER
);
var_export(array_column($m, 2, 1));
输出:
array (
'Bebouwde oppervlakte' => '80 M2',
'Terras oppervlakte' => '20 M2',
'Keuken oppervlakte' => '12 M2',
)