【问题标题】:My function accidentally removes numbers from string我的函数不小心从字符串中删除了数字
【发布时间】:2019-06-04 21:55:04
【问题描述】:

我的函数意外地从我的字符串中删除了数字,因为我希望它们仍然存在。怎么改?

必须不变的原始字符串; iPhone 7 plus 34 GB 10 英寸

echo implode(' ',array_unique(str_word_count('iPhone 7 plus 34 GB 10 inches',1)));

不幸的是,它返回: iPhone 加 GB 英寸

【问题讨论】:

  • 我不明白你的问题。这就是str_word_count() 的全部目的,它只返回单词。如果您想在作品中将数字作为可能的字母列入白名单,请使用第三个参数。例如, implode('', range(0,9)) 或类似的东西

标签: php arrays string substring


【解决方案1】:

您只需要按照语法在字符列表中添加 0..9,

echo implode(' ',array_unique(str_word_count('iPhone 7 plus 34 GB 10 inches',1,"0..9")));

Working demo.

语法

str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ) : mixed

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多