gpt4 book ai didi

php - 如何使用 PHP 正则表达式压缩空格字符?

转载 作者:行者123 更新时间:2023-12-04 14:15:36 25 4
gpt4 key购买 nike

$string = 'Hello    this is a bunch of numbers: 333 and letters and $pecial Characters@!*(';

$foo = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

echo $foo;

以上返回:

Hello    this is a bunch of numbers 333 and letters and pecial Characters

我想保留空格,但如果有多个空格则不会。怎么办?

它应该是这样的:

Hello this is a bunch of numbers 333 and letters and pecial Characters

最佳答案

$foo = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
$foo = preg_replace('/\s+/',' ',$foo);

关于php - 如何使用 PHP 正则表达式压缩空格字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5413536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com