【问题标题】:php explode() not working properly with special charactersphp explode() 无法使用特殊字符正常工作
【发布时间】:2013-03-30 07:32:50
【问题描述】:
$this->row['contents'] = strip_tags($this->row['contents']);
$this->words = explode(" ", $this->row['contents']);

上面的代码应该为$this->row['contents'] 的每个单词创建一个带有key => value 对的数组。在正常情况下它工作得很好,但是带有一个字符串,例如:

每升 10.40 美元。

它会分开

[0] => This
[1] => Costs U$
[2] => 10.40 per
[3] => liter.

有什么办法解决这个问题吗?

【问题讨论】:

  • 这对我来说很好用......$var = 'This costs U$ 10.40 per liter.'; echo var_export(explode(' ', $var), TRUE);
  • Hexdump 字符串并找出您认为的任何空格是否实际上是制表符或换行符,或不间断空格。
  • PHP 是不是让你被正则表达式炸飞了?如果是这样,请在\s上爆炸?
  • @Patashu 不使用常规的 explode 函数,您可以使用其中一个正则表达式函数。
  • @mario 你是对的,一个是制表符,另一个是换行符。

标签: php escaping special-characters explode


【解决方案1】:

也许这段代码对你有帮助

$this->words = preg_split('/\s+/', $this->row['contents']);

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多