如何在PHP中删除字符串中的所有空格? [重复]
本文翻译自: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_replace
比preg_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中删除字符串中的所有空格? [重复]相关推荐
- python删除字符串中的字母_在Python中删除字符串中的大写字母
我想写一个函数消除一个字符串和2个可选参数.第一个可选参数(bad_characters)取一个字母,第三个参数(case_sensitive)取一个布尔值.该函数应该接受一个字符串s并移除bad_c ...
- C语言中删除字符串中一个字母,C程序删除字符串中除字母之外的所有字符
C程序删除字符串中除字母之外的所有字符 在此示例中,您将学习从用户输入的字符串中删除除字母之外的所有字符. 要理解此示例,您应该了解以下C语言编程主题: 删除字符串中除字母之外的字符#include ...
- 如何在Python中删除字符串中的所有反斜杠?
result = 'https://www.baidu.com\/heihei.com' result = result.replace("\\", '') print(resul ...
- python字符串去掉空行_从python中的字符串中删除空格
python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...
- kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符
kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...
- c语言 去电txt空白行,删除字符串中多余的空白字符和空行(C语言实现)
要求:处理一个字符串,删除字符串中多余的空格.水平制表符和空行,并满足下列要求: (1)对原字符串只能进行一次扫描. (2)不允许申请新的空间. (3)处理后的字符串的首尾不能有空格.制表符和空行. ...
- python删除指定位置的字符串_python去除区域 python删除字符串中指定位置字符
python删除字符串中指定位置字符 原程序是这样的: ser = serial.Serial("/dev/ttyAMA0", 9600) def main字符串的话,你可以把他当 ...
- 删除字符串中出现次数最少的字符
在公交车上看一博客实现删除字符串中出现次数最少的字符,认为里面使用数组来作为hash很好,所以我就自己实现一遍. 要求:实现删除一个字符串中出现次数最少的字符.输出删除后的字符,要求安装原来顺序输出. ...
- python使用正则表达式删除字符串中的其它字符只保留数字和字母
python使用正则表达式删除字符串中的其它字符只保留数字和字母 #python使用正则表达式删除字符串中的其它字符只保留数字和字母 # Python code to demonstrate # to ...
最新文章
- 【分布式事务系列九】聊聊分布式事务
- OpenCV3.4.1+vs2017安装及配置
- c++学习笔记之多文件操作
- PhotoShop常用的功能汇总
- Java零基础如何去学习Java编程
- 在双系统(Windows与Ubuntu)下删除Ubuntu启动项
- 简单Nlp分析套路,获取数据(爬虫),数据处理(分词,词频,命名实体识别与关键词抽取),结果展现
- 从 AI、芯片到量子计算,阿里达摩院发布 2020 十大科技趋势
- 中科大c语言试卷,中科大考研试题.doc
- 2014百度之星资格赛——Disk Schedule
- 让能力变现,才是硬道理
- STM32最小系统使用FlyMcu烧写步骤
- c语言 停车管理系统
- Safari 安装 Tampermonkey(油猴)插件
- java 加法运算_[Java]大数运算之加法
- 珠海沙龙PPT已上传!
- ios 打包 验证应用失败原因汇总
- MindSpore实现手写数字识别
- [AFCTF2018]花开藏宝地
- [IAR] 编译报错:Variable expansion failed for Pre-Build command line