替换字段
Replace the field
我正在使用以下代码:
function my_map_field( $data ) {
$map = array(
'Toys for 12 years old' => '12+',
'Toys for 4 years old' => '4+',
'Toys for 7 years old' => '7+',
'Toys for 9 years old' => '9+',
);
return isset( $map[$data] ) ? $map[$data] : null;
}
它取代:12岁玩具---> 12+
如果字段包含 ('12 年')
,我需要将确切的文本映射 ('Toys for 12 years old') 更改为
例如:
如果该字段包含 12 年 写 12+;如果该字段包含 7 年 ,请填写 7+。如果没有找到,留空
谢谢
准确匹配单词
function my_map_field( $data ) {
$map = array(
'Toys for 12 years old' => '12+',
'Toys for 4 years old' => '4+',
'Toys for 7 years old' => '7+',
'Toys for 9 years old' => '9+',
);
foreach ($map as $text => $number) {
if (stripos($text, $data) !== false) {
return $map[$text];
}
}
return '';
}
echo my_map_field('7 years');// result is 7+
echo my_map_field('11 years');// result is empty
echo my_map_field('12 years');// result is 12+
匹配任意数字与年份[s]
function my_map_field( $data ) {
$map = array(
'Toys for 12 years old' => '12+',
'Toys for 4 years old' => '4+',
'Toys for 7 years old' => '7+',
'Toys for 9 years old' => '9+',
);
preg_match('/(\d+)\s*year[s]?/i', $data, $matches);
$matchNumber = ($matches[1] ?? '');
foreach ($map as $text => $number) {
if (stripos($text, $matchNumber) !== false) {
return $map[$text];
}
}
return '';
}
echo my_map_field('buy toys for 12 years old');// get 12+
echo my_map_field('used toys for 12 years old');// get 12+
echo my_map_field('buy toys for 15 years old');// get empty.
我正在使用以下代码:
function my_map_field( $data ) {
$map = array(
'Toys for 12 years old' => '12+',
'Toys for 4 years old' => '4+',
'Toys for 7 years old' => '7+',
'Toys for 9 years old' => '9+',
);
return isset( $map[$data] ) ? $map[$data] : null;
}
它取代:12岁玩具---> 12+
如果字段包含 ('12 年')
,我需要将确切的文本映射 ('Toys for 12 years old') 更改为例如: 如果该字段包含 12 年 写 12+;如果该字段包含 7 年 ,请填写 7+。如果没有找到,留空
谢谢
准确匹配单词
function my_map_field( $data ) {
$map = array(
'Toys for 12 years old' => '12+',
'Toys for 4 years old' => '4+',
'Toys for 7 years old' => '7+',
'Toys for 9 years old' => '9+',
);
foreach ($map as $text => $number) {
if (stripos($text, $data) !== false) {
return $map[$text];
}
}
return '';
}
echo my_map_field('7 years');// result is 7+
echo my_map_field('11 years');// result is empty
echo my_map_field('12 years');// result is 12+
匹配任意数字与年份[s]
function my_map_field( $data ) {
$map = array(
'Toys for 12 years old' => '12+',
'Toys for 4 years old' => '4+',
'Toys for 7 years old' => '7+',
'Toys for 9 years old' => '9+',
);
preg_match('/(\d+)\s*year[s]?/i', $data, $matches);
$matchNumber = ($matches[1] ?? '');
foreach ($map as $text => $number) {
if (stripos($text, $matchNumber) !== false) {
return $map[$text];
}
}
return '';
}
echo my_map_field('buy toys for 12 years old');// get 12+
echo my_map_field('used toys for 12 years old');// get 12+
echo my_map_field('buy toys for 15 years old');// get empty.