PHP - 包含特定文本标记的输出数组值
PHP - Output array value that contains a specific text marker
我正在使用一个函数将 Shopify 中的 collection 产品输出到 WordPress 页面上。
除了在 Shopify 中作为标签输入的自定义值外,我已正确显示大部分数据。使用 api,然后我尝试获取格式为 att:Subtitle 的产品副标题的特定标签:在每个产品自定义 value/text.
之前
这是我必须的代码(我在中间评论了其他不成功的尝试)-这是在整体代码中显示 Shopify 中的 4 种产品 collection:
// Using tags to output custom data from products
$tags = $current_product['tags'];
// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);
// Evaluate if there is a string with att:Subtitle in the product tags
// https://tecadmin.net/check-string-contains-substring-in-php/
$subtitle_attribute_key = "att:Subtitle:";
if (strpos($tags, 'att:Subtitle:') !== false) {
// Returns a numbered value corresponding to my subtitle attribute
// $key = array_search($subtitle_attribute_key, $product_tags);
$sub = strpos($tags, $subtitle_attribute_key);
// Turns the numbered value into a text value
// $numArray = explode(" ", $sub);
// var_dump($numArray);
$value = print_r($sub, true);
// $value = array_search("att:Subtitle:",$product_tabs);
// $value = array_search("att:Subtitle:", $tabs); // Warning: array_search() expects parameter 2 to be array, null given
// $result = $product_tags['$value']; // my attempt to return the text
// Remove att:Subtittle: in front of the subtitle value to output the clean final value
$subtitle = ltrim($value, 'att:Subtitle:');
}
到目前为止,我在显示 $subtitle 时显示的是数字值...但我不知道如何显示自定义文本值。
有什么想法吗?
谢谢
编辑:我正在处理具有多个标签的产品,但我无法控制这些标签。在标签中,我试图找到以 att:Subtitle: 开头的标签,但只在该标记后显示自定义值。
当我回显 $tags 时,ist 会出现如下内容:
att:Benefit:balance, att:Perfect:Combination Skin, att:Size: 1.8 oz, att:Subtitle: Multi-Tasking Product, Key Ingredient 1, Key Ingredient 2, Essentials, meta-related-product-xyz, meta-related-product-brandname
它们都有不同的标签列表
据我了解,您正试图从产品描述的标签中删除 'att:Subtitle:'?也许您应该尝试替换标签数组中的那个值
// Using tags to output custom data from products
$tags = $current_product['tags'];
// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);
// Get rid of attribute on product tag
for($i = 0; $i < count($product_tags); $i++){
$subtitle = str_replace(':att:Subtitle','',$product_tags[i]);
}
当然是数字。您正在从 strpos()
中获取一个数字,并最终尝试在其上 运行 ltrim()
。
如果您希望所需的文本紧跟在 'att:Subtitle:' 之后的 $tags
中并且该字符串中没有其他内容,那么 $subtitle = substr($tags, strpos('att:Subtitle:') + strlen('att:Subtitle:'));
应该会给您。如果您希望它可能是 $product_tags
数组中的一个元素,则需要循环(我假设它最多出现一次):
foreach ($product_tags as $product_tag) {
if (strpos($product_tag, 'att:Subtitle:') !== false) {
$subtitle = substr($product_tag, strpos($product_tag, 'att:Subtitle:') + strlen('att:Subtitle:'));
break;
}
}
我正在使用一个函数将 Shopify 中的 collection 产品输出到 WordPress 页面上。
除了在 Shopify 中作为标签输入的自定义值外,我已正确显示大部分数据。使用 api,然后我尝试获取格式为 att:Subtitle 的产品副标题的特定标签:在每个产品自定义 value/text.
之前这是我必须的代码(我在中间评论了其他不成功的尝试)-这是在整体代码中显示 Shopify 中的 4 种产品 collection:
// Using tags to output custom data from products
$tags = $current_product['tags'];
// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);
// Evaluate if there is a string with att:Subtitle in the product tags
// https://tecadmin.net/check-string-contains-substring-in-php/
$subtitle_attribute_key = "att:Subtitle:";
if (strpos($tags, 'att:Subtitle:') !== false) {
// Returns a numbered value corresponding to my subtitle attribute
// $key = array_search($subtitle_attribute_key, $product_tags);
$sub = strpos($tags, $subtitle_attribute_key);
// Turns the numbered value into a text value
// $numArray = explode(" ", $sub);
// var_dump($numArray);
$value = print_r($sub, true);
// $value = array_search("att:Subtitle:",$product_tabs);
// $value = array_search("att:Subtitle:", $tabs); // Warning: array_search() expects parameter 2 to be array, null given
// $result = $product_tags['$value']; // my attempt to return the text
// Remove att:Subtittle: in front of the subtitle value to output the clean final value
$subtitle = ltrim($value, 'att:Subtitle:');
}
到目前为止,我在显示 $subtitle 时显示的是数字值...但我不知道如何显示自定义文本值。
有什么想法吗?
谢谢
编辑:我正在处理具有多个标签的产品,但我无法控制这些标签。在标签中,我试图找到以 att:Subtitle: 开头的标签,但只在该标记后显示自定义值。
当我回显 $tags 时,ist 会出现如下内容:
att:Benefit:balance, att:Perfect:Combination Skin, att:Size: 1.8 oz, att:Subtitle: Multi-Tasking Product, Key Ingredient 1, Key Ingredient 2, Essentials, meta-related-product-xyz, meta-related-product-brandname
它们都有不同的标签列表
据我了解,您正试图从产品描述的标签中删除 'att:Subtitle:'?也许您应该尝试替换标签数组中的那个值
// Using tags to output custom data from products
$tags = $current_product['tags'];
// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);
// Get rid of attribute on product tag
for($i = 0; $i < count($product_tags); $i++){
$subtitle = str_replace(':att:Subtitle','',$product_tags[i]);
}
当然是数字。您正在从 strpos()
中获取一个数字,并最终尝试在其上 运行 ltrim()
。
如果您希望所需的文本紧跟在 'att:Subtitle:' 之后的 $tags
中并且该字符串中没有其他内容,那么 $subtitle = substr($tags, strpos('att:Subtitle:') + strlen('att:Subtitle:'));
应该会给您。如果您希望它可能是 $product_tags
数组中的一个元素,则需要循环(我假设它最多出现一次):
foreach ($product_tags as $product_tag) {
if (strpos($product_tag, 'att:Subtitle:') !== false) {
$subtitle = substr($product_tag, strpos($product_tag, 'att:Subtitle:') + strlen('att:Subtitle:'));
break;
}
}