【问题标题】:Need to Replace Colons and Forward Slashes in String with Nothing需要用空替换字符串中的冒号和正斜杠
【发布时间】:2013-09-08 05:19:31
【问题描述】:

这是我的代码:

add_filter('frm_validate_field_entry', 'my_function_name', 8, 3);
function my_function_name($errors, $posted_field, $posted_value)
{
   if($posted_field->id == 5)
   {
      $chbaa = strtolower($_POST['item_meta'][1]);
      $chbab = strtolower($_POST['item_meta'][2]);
      $chbac = strtolower($_POST['item_meta'][3]);
      $chbad = strtolower($_POST['item_meta'][4]);
      $_POST['item_meta'][5] = preg_replace('/\s+/', '', $chbaa) . preg_replace('/\s+/', '', $chbab) . preg_replace('/\s+/', '', $chbac) . preg_replace('/\s+/', '', $chbad);
   }
   return $errors;
}

目前这会删除不同字段中的所有空格并将所有内容都删除为小写,将四个字段组合成一个字符串。但是,字段 1 ($chbaa) 和 4 ($chbad) 分别包含正斜杠和冒号。字段 1 是日期,因此其格式为 00/00/0000,字段 4 是时间,因此其格式为 00:00:00。我需要从字符串中删除两个正斜杠和两个冒号,替换为空。

【问题讨论】:

    标签: php regex preg-replace str-replace


    【解决方案1】:

    我想通了。对于字段 1 和 4,我将 /\s+/ 替换为 /[^0-9]/,这样就可以了。

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多