本文翻译自:How do I strip all spaces out of a string in PHP? [duplicate]

Possible Duplicate: 可能重复:
To strip whitespaces inside a variable in PHP 在PHP中去除变量内的空格

How can I strip / remove all spaces of a string in PHP? 我怎么能剥夺 / 删除 PHP 字符串的所有空间

I have a string like $string = "this is my string"; 我有一个字符串,如$string = "this is my string";

The output should be "thisismystring" 输出应该是"thisismystring"

How can I do that? 我怎样才能做到这一点?


#1楼

参考:https://stackoom.com/question/8qjN/如何在PHP中删除字符串中的所有空格-重复


#2楼

Do you just mean spaces or all whitespace? 你只是指空间或所有空格吗?

For just spaces, use str_replace : 对于空间,使用str_replace :

$string = str_replace(' ', '', $string);

For all whitespace (including tabs and line ends), use preg_replace : 对于所有空格(包括制表符和行尾),请使用preg_replace :

$string = preg_replace('/\s+/', '', $string);

(From here ). (从这里开始 )。


#3楼

If you want to remove all whitespace: 如果要删除所有空格:

$str = preg_replace('/\\s+/', '', $str);

See the 5th example on the preg_replace documentation . 请参阅preg_replace文档中的第5个示例。 (Note I originally copied that here.) (注意我最初在这里复制了。)

Edit: commenters pointed out, and are correct, that str_replace is better than preg_replace if you really just want to remove the space character. 编辑:评论者指出,并且是正确的,如果你真的只想删除空格字符, str_replacepreg_replace更好。 The reason to use preg_replace would be to remove all whitespace (including tabs, etc.). 使用preg_replace的原因是删除所有空格(包括制表符等)。


#4楼

str_replace会这样做

$new_str = str_replace(' ', '', $old_str);

#5楼

If you know the white space is only due to spaces, you can use: 如果您知道空白区域仅由空格所致,您可以使用:

$string = str_replace(' ','',$string);

But if it could be due to space, tab...you can use: 但如果它可能是由于空间,标签......您可以使用:

$string = preg_replace('/\s+/','',$string);

如何在PHP中删除字符串中的所有空格? [重复]相关推荐

  1. python删除字符串中的字母_在Python中删除字符串中的大写字母

    我想写一个函数消除一个字符串和2个可选参数.第一个可选参数(bad_characters)取一个字母,第三个参数(case_sensitive)取一个布尔值.该函数应该接受一个字符串s并移除bad_c ...

  2. C语言中删除字符串中一个字母,C程序删除字符串中除字母之外的所有字符

    C程序删除字符串中除字母之外的所有字符 在此示例中,您将学习从用户输入的字符串中删除除字母之外的所有字符. 要理解此示例,您应该了解以下C语言编程主题: 删除字符串中除字母之外的字符#include ...

  3. 如何在Python中删除字符串中的所有反斜杠?

    result = 'https://www.baidu.com\/heihei.com' result = result.replace("\\", '') print(resul ...

  4. python字符串去掉空行_从python中的字符串中删除空格

    python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...

  5. kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

    kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...

  6. c语言 去电txt空白行,删除字符串中多余的空白字符和空行(C语言实现)

    要求:处理一个字符串,删除字符串中多余的空格.水平制表符和空行,并满足下列要求: (1)对原字符串只能进行一次扫描. (2)不允许申请新的空间. (3)处理后的字符串的首尾不能有空格.制表符和空行. ...

  7. python删除指定位置的字符串_python去除区域 python删除字符串中指定位置字符

    python删除字符串中指定位置字符 原程序是这样的: ser = serial.Serial("/dev/ttyAMA0", 9600) def main字符串的话,你可以把他当 ...

  8. 删除字符串中出现次数最少的字符

    在公交车上看一博客实现删除字符串中出现次数最少的字符,认为里面使用数组来作为hash很好,所以我就自己实现一遍. 要求:实现删除一个字符串中出现次数最少的字符.输出删除后的字符,要求安装原来顺序输出. ...

  9. python使用正则表达式删除字符串中的其它字符只保留数字和字母

    python使用正则表达式删除字符串中的其它字符只保留数字和字母 #python使用正则表达式删除字符串中的其它字符只保留数字和字母 # Python code to demonstrate # to ...

最新文章

  1. 【分布式事务系列九】聊聊分布式事务
  2. OpenCV3.4.1+vs2017安装及配置
  3. c++学习笔记之多文件操作
  4. PhotoShop常用的功能汇总
  5. Java零基础如何去学习Java编程
  6. 在双系统(Windows与Ubuntu)下删除Ubuntu启动项
  7. 简单Nlp分析套路,获取数据(爬虫),数据处理(分词,词频,命名实体识别与关键词抽取),结果展现
  8. 从 AI、芯片到量子计算,阿里达摩院发布 2020 十大科技趋势
  9. 中科大c语言试卷,中科大考研试题.doc
  10. 2014百度之星资格赛——Disk Schedule
  11. 让能力变现,才是硬道理
  12. STM32最小系统使用FlyMcu烧写步骤
  13. c语言 停车管理系统
  14. Safari 安装 Tampermonkey(油猴)插件
  15. java 加法运算_[Java]大数运算之加法
  16. 珠海沙龙PPT已上传!
  17. ios 打包 验证应用失败原因汇总
  18. MindSpore实现手写数字识别
  19. [AFCTF2018]花开藏宝地
  20. [IAR] 编译报错:Variable expansion failed for Pre-Build command line

热门文章

  1. 算法----左叶子之和
  2. 解决android Stadio 升级之后 出现乱码
  3. 第十五周项目二-洗牌(范形程序设计)
  4. React Native之箭头函数和延展操作符(...)
  5. 微信小程序问题解决方案
  6. 配置实验室计算机步骤,计算机网络实验指导书(new)
  7. percona mysql安装_mysql 安装 (percona)
  8. Spark Shuffle两种Manager
  9. postman test 脚本
  10. 【基本数据结构之'图'】