php - 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]})]
问候
解决方案
也可以使用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
推荐阅读
- javascript - 使用 Google Auth 登录 React Redux 问题
- sql-server-2008 - 合并两个具有非等价列的表
- c# - 将json字符串保存到txt文件时的序列化问题
- ios - Firebase 获取重复数据
- c# - Box collider里面的Box collider会影响游戏性能吗?
- powershell - 从powershell类中的方法内返回多个变量
- c# - 我应该如何用科学计数法(+301)解析一个非常大的数字?
- javascript - 导出为默认的多个导入模块
- android - 如何结合平移和缩放动画?
- c# - 找不到 Visual Studio 2015 构建类型或名称空间 cs0246