【问题标题】:Function to get variable content and strip variable to only return numbers获取变量内容和剥离变量只返回数字的函数
【发布时间】:2021-08-03 15:01:54
【问题描述】:

尝试从 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;
    
}

【问题讨论】:

  • 为什么你认为 preg_replace 返回一个对象?你有没有尝试访问php.net阅读功能描述?
  • 该函数可以只返回变量,如:return $new_output;。这行得通吗?
  • @GerarddeVisser 谢谢,这就是我所需要的。
  • @user2059376 这很好用。我已将此添加为答案,以供其他寻求此解决方案的人参考。随意接受/支持答案。

标签: php woocommerce preg-replace


【解决方案1】:

preg_replace 不会返回对象,也可以参考:https://www.php.net/manual/en/function.preg-replace.php

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2018-05-10
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多