Replacing Multiple Spaces with a Single Space: Deprecating ereg_replace
While using ereg_replace to replace multiple spaces with a single space may seem straightforward, it has been deprecated. Consequently, you may encounter errors when attempting to utilize it. This article presents an alternative solution.
Migration to preg_replace()
To replace ereg_replace, switch to preg_replace(). Instead of employing the [ \t\n\r] pattern, which matches multiple spaces, tabs, newlines, and carriage returns, use \s . This shorthand character class encompasses all whitespace characters, effectively replacing multiple spaces with a single space.
Code Example
Implement the following code to achieve the desired result:
$output = preg_replace('!\s !', ' ', $input);
Explanation
Additional Resource
Refer to the Regular Expression Basic Syntax Reference for further clarification on \d, \w, and \s character classes:
https://www.php.net/manual/en/regexp.reference.basic-syntax.php
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3