duangou2028 2018-05-01 13:18
浏览 79

PHP / Laravel 5.4 - 解析逗号分隔列表以允许相同的行或多行?

I am creating a tool to send emails and I want to allow for the option to write them in one line separated by a comma and space:

test@email.com, test2@email.com

OR multiple lines separated by a comma and space (which inadvertently creates more space):

test@email.com,
test2@email.com

The php code for the first option is easy:

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

Which creates an array like this when you multi-line it:

array:1 [
  0 => """
    test@email.com,

    test2@email.com
    """
]

Anyone know some simple logic/regex to allow for either? The first example of $this->data['emails'] comes as a string:

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

And the second has the special characters to account for:

"""
test@email.com, 

test2@email.com
"""
  • 写回答

2条回答 默认 最新

  • donglian1982 2018-05-01 13:55
    关注

    Alright, figured it out:

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

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计