首页 > 解决方案 > 获取变量内容和剥离变量以仅返回数字的函数

问题描述

尝试从 Woocommerce 注释的内容中提取运输跟踪号给客户,最终将其作为值传递给自定义字段

原始功能

    function get_value( $comment, $parameters ) {
    return $comment->content;
}

修改功能(不工作)

function get_value( $comment, $parameters ) {
    $tracking_note = $comment->content;
    $new_output = preg_replace( '/[^0-9]/', '', $tracking_note );
    return $new_output->content;
    
}

标签: phpwoocommercepreg-replace

解决方案


preg_replace 不会返回对象,另请参考:https ://www.php.net/manual/en/function.preg-replace.php 。

只需返回变量,如:return $new_output;


推荐阅读