首页 > 解决方案 > Wp All Import - 从标题中排除产品 ID

问题描述

我想通过 XML 导入产品,但产品在标题和标题处有 id,

这是一个简短的标题,例如:“Levis bootcut jean 5450313”,

id 是“5450313”,

我如何排除产品 ID 并导入清晰的标题:“Levis bootcut jean”,

我有一个示例函数,但我不知道如何针对我的情况进行修改:

function my_fix_title( $title ) {
$title = explode( ' ', $title );
array_pop( $title );
return implode( ' ', $title ); }

Wp All Import 像这样调用函数:[my_fix_title({product_name[1]})]

问候

标签: phpwpallimport

解决方案


也可以使用preg_replace

$title1 = 'some product 24556';
$title2 = 'another product 56789';

function my_fix_title( $title ) {
    $fixedTitle = preg_replace('/[ ]\d+$/', '', $title);
    return $fixedTitle;
}

echo my_fix_title($title1);
echo '<br>';
echo my_fix_title($title2);

输出:

some product
another product

示例小提琴

'/[ ]\d+$/'解释:

/   // Start pattern
[ ] // A space
\d+ // One or more digits
 $  // At the end of the string
/   // End pattern

推荐阅读