在php中格式化数据

发布于 2024-07-21 03:10:32 字数 344 浏览 3 评论 0原文

我需要做的是格式化变量中的数据,如下所示:

format: xxx-xxx variable: 123456 output: 123-456

问题是我需要能够更改格式,因此静态解决方案不起作用。 我也希望能够更改变量大小,例如:

format: xxx-xxx variable: 1234 output: 1-234

注意:所有变量都是数字

编辑 我应该清楚格式,它不会总是以 3 分组,并且它可能有比“-”更多的符号,组将不稳定 1-22-333-4444 它只会按 1-5 分组

What I need to be able do is format data in a variable, like so:

format: xxx-xxx variable: 123456 output: 123-456

The problem is I need to be able to change the format, so a static solution wouldn't work. I also like to be able to change the variable size, like:

format: xxx-xxx variable: 1234 output: 1-234

Note: All of the variables will be numbers

Edit I should have been clear on the format its not going to always be grouping of 3, and it may have more then "-" as a symbol, the groups will be inconstant 1-22-333-4444 it will only be in grouping of 1-5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮萍、无处依 2024-07-28 03:10:32

最好的选择是 preg_replace

正则表达式需要一些时间来适应,但这可能是您最好的选择...

编辑:

//initial parsing
$val = preg_replace(
    '/(\d*?)(\d{1,2}?)(\d{1,3}?)(\d{1,4})$/', 
    '${1}-${2}-$[3}-${4}', 
    $inputString
);

//nuke leading dashes
$val - preg_replace('^\-+', '', $val);

关键是使除了最右边的一组之外的每组都非贪婪,从而允许右-面向侧面的模式匹配。

Your best bet is preg_replace.

Regular expressions take some getting used to, but this is probably your best bet...

EDIT:

//initial parsing
$val = preg_replace(
    '/(\d*?)(\d{1,2}?)(\d{1,3}?)(\d{1,4})$/', 
    '${1}-${2}-$[3}-${4}', 
    $inputString
);

//nuke leading dashes
$val - preg_replace('^\-+', '', $val);

The key is to make every set, except the righ-most one non-greedy, allowing for a right-side oriented pattern match.

浮世清欢 2024-07-28 03:10:32

如果您要格式化的输入始终是整数,您可以尝试 number_format 并将格式设置为金钱(千等..)
这是一个解决方案,它可以接受任何字符串并将其转换为您想要的格式:

$split_position = 3;
$my_string      = '';
echo strrev(implode('-',(str_split(strrev($my_string),$split_position))));

input: 1234;     output: 1-234
input: abcdefab; output: ab-cde-fab
input: 1234567   output: 1-234-567

If the input you are formatting is always an integer you can try number_format and format as money (thousands etc..)
Here is a solution which takes any string and turns it into your desired format:

$split_position = 3;
$my_string      = '';
echo strrev(implode('-',(str_split(strrev($my_string),$split_position))));

input: 1234;     output: 1-234
input: abcdefab; output: ab-cde-fab
input: 1234567   output: 1-234-567
欲拥i 2024-07-28 03:10:32

您可以实现策略模式,并拥有可在运行时交换的新格式化类。 如果您以前没有见过它,它看起来很复杂,但它确实有助于提高可维护性,并允许您随时使用 setFormatter() 切换格式化程序。

class StyleOne_Formatter implements Formatter
{
    public function format($text)
    {
      return substr($text,0,3).'-'.substr($text,3);
    }
}

class StyleTwo_Formatter implements Formatter
{
    public function format($text)
    {
      return substr($text,0,1).'-'.substr($text,1);
    }
}

然后你就会有你的格式化类,如下所示:

class NumberFormatter implements Formatter
{

   protected $_formatter = null;

   public function setFormatter(Formatter $formatter)
   {
      $this->_formatter = $formatter;
   }

   public function format($text)
   {
     return $this->_formatter->format($text);
   }
}

然后你可以像这样使用它:

 $text = "12345678910";
 $formatter = new NumberFormatter();

 $formatter->setFormatter(new StyleOne_Formatter());
 print $formatter->format($text);
 // Outputs 123-45678910

 $formatter->setFormatter(new StyleTwo_Formatter());
 print $formatter->format($text);
 // Outputs 1-2345678910

You could implement the strategy pattern and and have new formatting classes that are swappable at run time. It looks complicated if you haven't seen it before but it really helps with maintainability and allows you to switch the formatter with the setFormatter() at any time.

class StyleOne_Formatter implements Formatter
{
    public function format($text)
    {
      return substr($text,0,3).'-'.substr($text,3);
    }
}

class StyleTwo_Formatter implements Formatter
{
    public function format($text)
    {
      return substr($text,0,1).'-'.substr($text,1);
    }
}

Then you would have your formatting class which would be like so:

class NumberFormatter implements Formatter
{

   protected $_formatter = null;

   public function setFormatter(Formatter $formatter)
   {
      $this->_formatter = $formatter;
   }

   public function format($text)
   {
     return $this->_formatter->format($text);
   }
}

Then you could use it like this:

 $text = "12345678910";
 $formatter = new NumberFormatter();

 $formatter->setFormatter(new StyleOne_Formatter());
 print $formatter->format($text);
 // Outputs 123-45678910

 $formatter->setFormatter(new StyleTwo_Formatter());
 print $formatter->format($text);
 // Outputs 1-2345678910
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文