Files
stemcell/test.php
T
2026-09-15 10:26:34 +08:00

56 lines
2.0 KiB
PHP

<?php
function parseAdditionalAttributes($additionalAttributesString)
{
if (empty($additionalAttributesString)) {
return [];
}
$attributes = [];
$length = strlen($additionalAttributesString);
$i = 0;
while ($i < $length) {
// 寻找下一个属性名(字母数字下划线组成)
if (preg_match('/([a-zA-Z0-9_]+)=/', $additionalAttributesString, $matches, PREG_OFFSET_CAPTURE, $i)) {
$attributeName = $matches[1][0];
$nameStart = $matches[1][1];
$valueStart = $nameStart + strlen($attributeName) + 1; // +1 for '='
// 寻找下一个属性开始位置来确定当前属性值的结束位置
$nextAttrPos = null;
if (preg_match('/,([a-zA-Z0-9_]+)=/', $additionalAttributesString, $nextMatches, PREG_OFFSET_CAPTURE, $valueStart)) {
$nextAttrPos = $nextMatches[0][1]; // 逗号的位置
}
// 提取属性值
if ($nextAttrPos !== null) {
$value = substr($additionalAttributesString, $valueStart, $nextAttrPos - $valueStart);
$i = $nextAttrPos + 1; // 跳过逗号
} else {
// 最后一个属性,取到字符串末尾
$value = substr($additionalAttributesString, $valueStart);
$i = $length; // 结束循环
}
//去掉导入数据中的_X000D_
$attributes[mb_strtolower(trim($attributeName))] = preg_replace('/_x([0-9a-fA-F]{4})_/', '', trim($value));
;
} else {
// 没有找到更多属性,结束
break;
}
}
return $attributes;
}
$attribute = 'document_date=9/27/19,document_version=1.1.0,document_path=/media/files/minireview/MRDX20359CN-Airway_Epithelial_Cells.pdf,parent_skus=00218,05001,05021,05022,05008,05040,00218CA.2';
$str = '';
foreach (parseAdditionalAttributes($attribute) as $key => $value) {
$str .= $key . '=' . $value . ',';
}
echo rtrim($str, ',').PHP_EOL;