【问题标题】:Can't remove space from string无法从字符串中删除空格
【发布时间】:2015-08-07 08:17:37
【问题描述】:

我有一个数组,其中填充了从名为“words.txt”的文件中获取的元素。访问数组的第一个元素后,我似乎无法修改它以在其他地方使用。我只是想从我正在访问的元素中删除一个空格,但即使在使用str_replace() 之后,它也会与一个空格相呼应。

$words = file("words.txt");

$domain = "www." . $words[0] . ".com";
$domain = str_replace(' ', '', $domain);

echo $domain;

我在这里缺少什么简单的东西?

【问题讨论】:

  • var_dump($words) 看看你得到了什么。
  • 始终最好将您的数据 var_dump 以供我们查看,以及您获得的实际输出。

标签: php string file-io


【解决方案1】:

file 默认留在行尾(例如\n)。换行符在 HTML 中仅呈现为空格。您要么需要str_replace("\n", ..),要么使用file(.., FILE_IGNORE_NEW_LINES)

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2017-11-20
    相关资源
    最近更新 更多