问题描述
我正在尝试用一个空格替换多个空格.当我使用 ereg_replace
时,我收到一个关于它被弃用的错误.
I'm trying to replace multiple spaces with a single space. When I use ereg_replace
, I get an error about it being deprecated.
ereg_replace("[
]+", " ", $string);
是否有相同的替代品.我需要用一个空格替换多个 " "
空格和多个 nbsp
空格.
Is there an identical replacement for it. I need to replace multiple " "
white spaces and multiple nbsp
white spaces with a single white space.
推荐答案
使用 preg_replace()
而不是 []
使用 s
:
Use preg_replace()
and instead of []
use s
:
$output = preg_replace('!s+!', ' ', $input);
来自正则表达式基本语法参考:
d、w 和 s
速记字符类匹配数字、单词字符(字母、数字和下划线),和空格(空格、制表符和行断).可以在里面使用字符类之外.
Shorthand character classes matching digits, word characters (letters, digits, and underscores), and whitespace (spaces, tabs, and line breaks). Can be used inside and outside character classes.
这篇关于php用一个空格替换多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!