问题描述
我想通过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]})]
致谢
解决方法
array_pop删除数组的最后一个元素并返回它。因此,我们可以简单地从array_pop中获取返回值,并将其发送回调用方函数。请尝试关注
function my_fix_title( $title ) {
$title = explode( ' ',$title );
$id = array_pop( $title );
return $id;
}
更新: 由于OP的缓存问题以及他的方法工作正常,因此该答案不再有效。
,也可以使用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
示例fiddle
'/[ ]\d+$/'
的解释:
/ // Start pattern
[ ] // A space
\d+ // One or more digits
$ // At the end of the string
/ // End pattern