Home >Backend Development >PHP Problem >How to remove all spaces in php

How to remove all spaces in php

王林
王林Original
2021-07-06 14:56:523670browse

php去掉所有的空格的方法是,使用正则表达式来实现,例如【preg_replace('# #','',$goodid)】。preg_replace函数会执行一个正则表达式的搜索和替换。

How to remove all spaces in php

本文操作环境:windows10系统、php 7.3、thinkpad t480电脑。

在php中如果我们要去掉字符串中所有的空格,最快捷有效的方式就是使用正则表达式。下面我们就来一起看下。

preg_replace 函数执行一个正则表达式的搜索和替换。

举个小例子:

<?php
$string = &#39;google 123, 456&#39;;
$pattern = &#39;/(\w+) (\d+), (\d+)/i&#39;;
$replacement = &#39;phpphp ${2},$3&#39;;
echo preg_replace($pattern, $replacement, $string);
?>

运行结果:

phpphp 123,456

下面我们使用正则匹配去除所有的空格

preg_replace(&#39;# #&#39;,&#39;&#39;,$goodid)

相关视频教程分享:php视频教程

The above is the detailed content of How to remove all spaces in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn