【问题标题】:PHP/Laravel 5.4 - Parsing a comma separated list to allow for same line OR multiple lines?PHP/Laravel 5.4 - 解析逗号分隔列表以允许同一行或多行?
【发布时间】:2018-10-11 12:51:16
【问题描述】:

我正在创建一个发送电子邮件的工具,我希望允许选择将它们写在一行中,用逗号和空格分隔:

test@email.com, test2@email.com

或用逗号和空格分隔的多行(无意中创建了更多空格):

test@email.com,
test2@email.com

第一个选项的php代码很简单:

$addresses = explode(', ', $this->data['emails']); 

当你多行它时,它会创建一个这样的数组:

array:1 [
  0 => """
    test@email.com,\n
    test2@email.com
    """
]

有人知道一些简单的逻辑/正则表达式来允许吗? $this->data['emails'] 的第一个例子是一个字符串:

"test@email.com, test2@email.com"

第二个有特殊字符要考虑:

"""
test@email.com, \n
test2@email.com
"""

【问题讨论】:

    标签: php laravel list textarea explode


    【解决方案1】:

    好吧,想通了:

        if (strpos($this->data['emails'], "\n")) {
            $addresses = explode(", \n", $this->data['emails']);
        } else {
            $addresses = explode(',', $this->data['emails']);
        }
    

    【讨论】:

      【解决方案2】:

      你可以用这个:

      $addresses = array_map('trim', explode(',', $this->data['emails']));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-03
        • 1970-01-01
        相关资源
        最近更新 更多