我需要一些PHP的帮助,因为我对它很陌生。
我有一组来自某个位置的字符串,我需要有条件地从它中提取字符串子集,无论何时出现。
设置:
我得到的每个字符串都有Attributes
,每个Attribute
都有一些Values
。所有Attributes
都是可选的,所有Attributes
都可以包含可变数量的值。
这些字符串的模式是这样的:
... Attribute Name: Attribute Values Attribute Name: Attribute Values ...
例如,以下是一些示例字符串:
Attribute A: Value 1, Value 2 Attribute B: Value 3, Value 4, Value 5, Value 6 Attribute C: Value 7, Attribute D: Value 8, Value 9
Attribute A: Value 1, Value 2, Value 3 Attribute B: Value 4, Value 5 Attribute C: Value 6, Value 7, Value 8, Attribute E: Value 9, Value 10, Value 11
Attribute B: Value 1, Value 2, Value 3 Attribute C: Value 4 Attribute D: Value 5, Value 6, Value 7, Value 8 Attribute E: Value 9, Value 10
请注意,某些字符串中缺少一些Attributes
,并且所有Attributes
旁边都会列出可变数量Values
。
问题:
每当Attribute D
Values
出现时,我想提取Attribute D
的所有值。
所以例如......
输入此字符串:
Attribute A: Value 1, Value 2 Attribute B: Value 3, Value 4, Value 5, Value 6 Attribute C: Value 7, Attribute D: Value 8, Value 9
我应该得到这个输出:Value 8, Value 9
输入此字符串:
Attribute A: Value 1, Value 2, Value 3 Attribute B: Value 4, Value 5 Attribute C: Value 6, Value 7, Value 8, Attribute E: Value 9, Value 10, Value 11
我应该得到这个输出:空字符串或空字符串,因为输入字符串中不存在Attribute D
输入此字符串:
Attribute B: Value 1, Value 2, Value 3 Attribute C: Value 4 Attribute D: Value 5, Value 6, Value 7, Value 8 Attribute E: Value 9, Value 10
我应该得到这个输出:Value 5, Value 6, Value 7, Value 8
我认为这里需要一系列PHP ifs和字符串函数,但是我想知道是否有人可以记录条件提取代码..会加速我的速度:)
自己编写代码:
我首先使用:
作为分隔符将字符串分解为子字符串。然后我遍历每个子字符串并搜索Attribute D
字符串。如果找到,我知道下一个子字符串将具有Attribute D
的值。然后我只删除下一个子字符串中的最后一个字(即删除下一个Attribute's
名称)。
这段代码中有2个漏洞(虽然不会出现在我的环境中)..希望你们能找到它们:)
以下是我使用的最终代码:
<?php
$subStrings = explode(':',$product['description']);
$arrayIndexContainingAttributeD = -1;
$length = count($subStrings);
for ($i = 0; $i < $length; $i++)
{
if(strpos($subStrings[$i], 'Attribute D') != false)
{
$arrayIndexContainingAttributeD = $i + 1;
break;
}
}
$attributeDStringAvailable = false;
if($arrayIndexContainingAttributeD != -1 && $arrayIndexContainingAttributeD<$length)
{
$attributeDString = preg_replace('/\W\w+\s*(\W*)$/', '$1', $subStrings[$arrayIndexContainingAttributeD]);
$attributeDStringAvailable = true;
}
?>
<?php if ($attributeDStringAvailable == true) { ?>
<h3><a href="<?php echo $product['href']; ?>"><?php echo $attributeDString; ?></a></h3>
<?php } ?>
答案 0 :(得分:0)
试图让它尽可能不言自明。例如:
$string = 'Attribute B: Value 1, Value 2, Value 3 Attribute C: Value 4 Attribute D: Value 5, Value 6, Value 7, Value 8 Attribute E: Value 9, Value 10';
$query = 'D';
echo getAttributeValues($string, $query);
function getAttributeValues($string = '', $query = null) {
if(!strpos($string, $query)) {
echo 'attribute not in string';
} else {
$array = explode('Attribute ', $string);
$attributes = array_filter($array);
foreach($attributes as $attribute) {
if(strpos($attribute, $query.':') !== false) {
$return = str_replace($query.':', '', $attribute);
return trim($return);
}
}
}
}
答案 1 :(得分:0)
自己编写代码:
我首先将字符串分解为子字符串,使用:作为分隔符。然后我遍历每个子字符串并搜索属性D字符串。如果找到,我知道下一个子字符串将具有属性D的值。然后我只删除下一个子字符串中的最后一个字(即删除下一个属性的名称)。
这段代码中有2个漏洞(虽然不会出现在我的环境中)..希望你们能找到它们:)
以下是我使用的最终代码:
<?php
$subStrings = explode(':',$product['description']);
$arrayIndexContainingAttributeD = -1;
$length = count($subStrings);
for ($i = 0; $i < $length; $i++)
{
if(strpos($subStrings[$i], 'Attribute D') != false)
{
$arrayIndexContainingAttributeD = $i + 1;
break;
}
}
$attributeDStringAvailable = false;
if($arrayIndexContainingAttributeD != -1 && $arrayIndexContainingAttributeD<$length)
{
$attributeDString = preg_replace('/\W\w+\s*(\W*)$/', '$1', $subStrings[$arrayIndexContainingAttributeD]);
$attributeDStringAvailable = true;
}
?>
<?php if ($attributeDStringAvailable == true) { ?>
<h3><a href="<?php echo $product['href']; ?>"><?php echo $attributeDString; ?></a></h3>
<?php } ?>
答案 2 :(得分:0)
您可以使用单个正则表达式执行此操作:
$string = 'Attribute A: Value 1, Value 2 Attribute B: Value 3, Value 4, Value 5, Value 6 Attribute C: Value 7, Attribute D: Value 8, Value 9
Attribute A: Value 1, Value 2, Value 3 Attribute B: Value 4, Value 5 Attribute C: Value 6, Value 7, Value 8, Attribute E: Value 9, Value 10, Value 11
Attribute B: Value 1, Value 2, Value 3 Attribute C: Value 4 Attribute D: Value 5, Value 6, Value 7, Value 8 Attribute E: Value 9, Value 10';
preg_match_all('#Attribute D:\s*(.*?)(?:\s*(?:Attribute|$))#s', $string, $m);
print_r($m[1]);
<强>输出:强>
Array
(
[0] => Value 8, Value 9
[1] => Value 5, Value 6, Value 7, Value 8
)